Exploring Smartphone Influence on the Brain: Neurological Perspectives
Angular vs. React: Which One Should You Choose?
Discover Serenity: Your Guide to Mindfulness Apps
Discover how smartphone apps can enhance your mindfulness practice: 5 Senses Exercise with Apps
Discover How Smartwatches Measure Blood Pressure: Explained Simply
13 Exciting Games Launching in April 2024: From Freedom Planet 2 to TopSpin 2K25!
Discover the 5 Amazon Big Spring Deals on Tablets from Samsung, Lenovo
Big Savings Alert: Amazfit Smart Watches Now on Sale on Amazon!
Amazon's Big Spring Sale: Top 6 Anker Souncore Headphones and Earbuds Deals
Affordable VR Adventures: The Best Budget VR Headsets
Fly in Peace: Discover the Ultimate Noise-Cancelling Headphones for Flying
Bringing AI to Life: NVIDIA's Digital Human Technolgies in Healthcare, Gaming, and More
Discover Exciting New Games on NVIDIA GeForce NOW!
Steam Spring Sale 2024 is here: Explore the 10 Best FPS Games
The Future of iPhones: Apple's Bold Step into AI with DarwinAI
Discover the Magic of Sonos Soundbar: Transform Your Home Entertainment Experience!
Enhance Your Home Fun: 5 Best Sonos Soundbars to Amp Up Your Entertainment!
Pinterest Introduces AI-Powered Body Type Ranges for Inclusive Searches
Embrace the Next Wave: 35+ AI Tools for Enhanced Productivity in 2024
Xbox Gaming Bonanza: Lots of New Games with Xbox Game Pass!
Sony Paves the Way for Gaming Evolution with 'Super-Fungible Tokens' Patent
Smart Printing Choices: 10 Key Factors to Consider When Buying an HP Printer or Any Printer
Projector Picks: Explore the Best Mini Projectors for Big Screen Fun!
JavaScript Essentials: Your Quick Start to Web Coding
Gaming Laptop Guide 2024: 10 Crucial Checks Before You Buy + Top 5 Picks for you!
Gaming Joy Awaits: Explore the Best PS5 Games of 2024
Epic Games Special: Dive into Astro Duel 2 for FREE this week. See What’s Coming Next Week!
Fitbit Fitness Tracker Guide 2024: Choose Your Perfect Fit
Feel the Beat: Exploring Top Over-Ear Headphones
Explore the Web Development Strategies in 2024: A Developers Handbook
Explore Must-Play Nintendo Switch Games in 2024!
Eclipse Ready: CE and ISO Certified Solar Eclipse Glasses for a Safe Sky Spectacle
Disney and Pixar’s Inside Out 2 Introduces New Emotions to Riley's World
Discover Waze's cool new features for safer and happier drives!
Discover the Top Picks: Best Smartwatches for Your Lifestyle
Discover the Best Smartphones Trending Now: Your Easy Guide to the Best Picks!
Sound Revolution: Discover the Best Bluetooth Speakers of 2024!
Discover the 10 Best Productivity Apps to Supercharge Your Daily Tasks
Discover,Install and Enjoy: The Best Chrome Extensions for Developers in 2024
Crack the Code: Your Guide to Computer Programming Magic in 2024
Boost Your Content Creation: 10 ChatGPT Prompts to Supercharge Content Creation Productivity
10 Best Tech Companies in Silicon Valley
Top 10 Web Development Interview Questions you can...
Learn how to Answer Tell me about Yourself
5 Books You Need to Read Right Now
25 Practical Ways to Earn Money Online
Translate this page in your preferred language:
Article by: Manish Methani
Last Updated: September 22, 2021 at 2:04pm IST
In C programming language, a string is a sequence of characters terminated by the null character '�'. String handling functions in C are a set of built-in functions that are used to perform operations on strings, such as finding the length of a string, copying a string, concatenating two strings, comparing two strings, and searching for a substring in a string.
The following are some of the most commonly used string handling functions in C:
char string_name[size];
int strlen(char *string);
char* strcpy(char *destination, const char *source);
char* strcat(char *destination, const char *source);
int strcmp(const char *string1, const char *string2);
char* strstr(const char *string1, const char *string2);
To declare a string in C, you can use the following syntax:
char string_name[size];
Here, string_name
is the name of the string, and size
is the maximum number of characters that the string can hold.
The strlen()
function is used to find the length of a string. It takes a string as input and returns the length of the string.
int strlen(char *string);
Here's an example of how to use the strlen()
function:
#include <stdio.h> #include <string.h> int main() { char name[50] = "John Doe"; int length = strlen(name); printf("The length of the string '%s' is %d ", name, length); return 0; }
Output:
The length of the string 'John Doe' is 8
The strcpy()
function is used to copy one string to another. It takes two strings as input - the destination string and the source string - and copies the contents of the source string to the destination string.
char* strcpy(char *destination, const char *source);
Here's an example of how to use the strcpy() function:
#include <stdio.h> #include <string.h> int main() { char source[] = "Hello, World!"; char destination[20]; strcpy(destination, source); printf("The source string is '%s'", source); printf("The destination string is '%s'", destination); return 0; }
Output:
The source string is 'Hello, World!' The destination string is 'Hello, World!'
The strcat()
function is used to concatenate two strings. It takes two strings as input - the destination string and the source string - and concatenates the contents of the source string to the end of the destination string.
char* strcat(char *destination, const char *source);
Here's an example of how to use the strcat() function:
#include <stdio.h> #include <string.h> int main() { char destination[50] = "Hello, "; char source[] = "World!"; strcat(destination, source); printf("The concatenated string is '%s'", destination); return 0; }
Output:
The concatenated string is 'Hello, World!'
The strcmp()
function is used to compare two strings. It takes two strings as input - string1
and string2
- and returns an integer value that indicates whether the two strings are equal, or whether one string is greater than or less than the other.
int strcmp(const char *string1, const char *string2);
Here's an example of how to use the strcmp()
function:
#include <stdio.h> #include <string.h> int main() { char string1[] = "apple"; char string2[] = "banana"; int result = strcmp(string1, string2); if (result == 0) { printf("The two strings are equal"); } else if (result < 0) { printf("'%s' is less than '%s'", string1, string2); } else { printf("'%s' is greater than '%s'", string1, string2); } return 0; }
Output:
'apple' is less than 'banana'
The strstr()
function is used to search for a substring in a string. It takes two strings as input - the main string and the substring to be searched for - and returns a pointer to the first occurrence of the substring in the main string.
char* strstr(const char *string1, const char *string2);
Here's an example of how to use the strstr() function:
#include <stdio.h> #include <string.h> int main() { char string1[] = "The quick brown fox jumps over the lazy dog"; char string2[] = "brown"; char *result = strstr(string1, string2); if (result == NULL) { printf("Substring not found"); } else { printf("Substring '%s' found at position %ld", string2, result - string1 + 1); } return 0; }
Output:
Substring 'brown' found at position 10
A string in C is an array of characters that ends with a null character " ".
You can use the "strlen()" function to find the length of a string in C.
You can use the "strcpy()" function to copy one string to another in C.
You can use the "strcat()" function to concatenate two strings in C.
You can use the "strcmp()" function to compare two strings in C.
You can use the "strstr()" function to search for a substring in a string in C.
That's it for this tutorial on string-handling functions in C programming. We hope you found it helpful! If you have any questions or feedback, please feel free to leave a comment below.
Here's a summary of the string-handling functions covered in this tutorial:
Function | Description |
---|---|
strlen() |
Returns the length of a string |
strcpy() |
Copies one string to another |
strcat() |
Concatenates two strings |
strcmp() |
Compares two strings |
strstr() |
Searches for a substring in a string |
Test your C Programming skills with this comprehensive mock test on C Programming.
Take TestSolve most asked Interview Questions on Flutter and Test your foundational skills in flutter.
Take TestSolve most asked GATE Questions in Operating Systems and test your Gate Score.
Take TestThis is a mock test designed to help you assess your knowledge and skills in HTML and CSS.
Take TestSolve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.
Take Test