Introduction
String manipulation is a fundamental aspect of programming in C, offering powerful ways to handle and process text data. Unlike higher-level languages, C requires direct management of strings using arrays and pointers, making understanding these operations crucial for efficient and effective coding. This blog delves into the core concepts and techniques of string manipulation in C, including string handling functions, memory management, and practical examples.

String Functions in C
String functions in C are essential tools for manipulating and managing text. They include operations such as copying (strcpy), concatenation (strcat), comparison (strcmp), and finding the length of a string (strlen). These functions handle strings as arrays of characters and require careful memory management to avoid common pitfalls like buffer overflow and segmentation faults.
strcat()
The strcat (string1,string2) function takes two string values as its arguments and appends string2 at the end of string1. The strcat() function basically replaces the terminating character at the end of string1 with the first character of string2, i.e., it will append a copy of string2 in string1.
Syntax
strcat(string1,string2);
Example
Output
String-1 before concatenation : Hello
String-1 after concatenation : Hello World!!
strchr()
The strchr() function takes two arguments: a string and a character. The strchr() function returns a pointer to the first occurrence of the character in the string. The function is present in ‘cstring.h’ header file.
Syntax
strchr(string1,ch);
Example
Output
The string after first occurrence of i in the string Coding Ninjas! : ing Ninjas!
If the character passed along with the string is not present in the string, then the function returns null.
Example
Output
The string after first occurrence of x in the string Coding Ninjas! is (null)
strrchr()
The strrchr() function takes two arguments: a string and a character. The strrchr() function is used to return a pointer to the last occurrence of the character in the string. The function is present in ‘cstring.h’ header file.
Syntax
strrchr(string1,ch);
Example
Output
The string after last occurrence of j in the string Coding Ninjas! is jas!
If the character passed along with the string is not present in the string, then the function returns null.
Example
Output
The string after last occurrence of x in the string Coding Ninjas! is (null)
strcmp()
The strcmp() function takes two strings as its argument and compares them lexicographically. The function strcmp(string1,string2) compares the string1 and string2 character by character beginning with the first character. The comparisons are made until characters in both strings are equal, or a null character is encountered.
Syntax
strcmp(string1,string2);
The function returns an integer value depending on the comparison :
A value 0 if both strings are identical
Example
Output
On comparing Ninja and Ninja the value returned is 0
A value greater than zero if the first not matching character in string1 has a greater ASCII value than the corresponding character in string2
Example
Output
On comparing ninja and Ninja the value returned is 32
Also see, Tribonacci Series and Short int in C Programming
A value less than zero if the first not matching character in string1 has an ASCII value lesser than the corresponding character in string2.
Example
Output
On comparing Ninja and ninja the value returned is -32
strlen()
The strlen() function takes a single string as the argument. The function is used to calculate the length of the string passed as the argument. While calculating the length of the string, it does not count the null character.
Syntax
strlen(string1);
Example
Output
Length of CodingStudio is 12
strcpy()
The strcpy() function takes two strings as its arguments. The function is used to copy one string to another, i.e., it copies string2 to string1.
Syntax
strcpy(string1,string2);
Example
Output
String-1 before strcpy() function : Hello
String-1 after strcpy() function : World
strlwr()
The strlwr() function takes a single string as its argument. The function is used to convert the passed string into lowercase.
Syntax
strlwr(string1);
strupr()
The strupr() function takes a single string as its argument. The function is used to convert the passed string into uppercase.
Syntax
strupr(string1);
strstr()
The strstr() function takes two strings as its arguments. The function strstr(string1,string2) is used to find the first occurrence of string2 in string1.
Syntax
strstr(string1,string2);
Example
Output
The string after the first occurrence of Coders in Hi Coders,All the best!!! is : Coders,All the best!!!




