Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
String Functions
2.1.
strcat()
2.2.
strchr()
2.3.
strrchr()
2.4.
strcmp()
2.4.1.
A value 0 if both strings are identical 
2.4.2.
A value greater than zero if the first not matching character in string1 has a greater ASCII value than the corresponding character in string2 
2.4.3.
A value less than zero if the first not matching character in string1 has an ASCII value lesser than the corresponding character in string2.
2.5.
strlen()
2.6.
strcpy()
2.7.
strlwr()
2.8.
strupr()
2.9.
strstr()
2.10.
Few More functions
3.
Frequently Asked Questions
3.1.
Whats the difference between strchr() and strrchr()?
3.2.
Is null character included in the length calculated by strlen()?
3.3.
What is the function of strset()?
4.
Conclusion
Last Updated: Jun 26, 2024
Easy

String Manipulation in C

Author Pallavi singh
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

A string is defined as a one-dimensional array of characters that always terminates with a special character ‘\0’ (NULL). Since each character takes one byte of memory and a string always terminates with a null character, the size of the character array used to store the string is always one greater than the number of characters in the string/word to be stored.

string manipulation in c

String Functions

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

#include<stdio.h>  
#include<string.h>

int main()  
{  
  char string1[]="Hello ";
  char string2[]="World!!";

  printf("String-1 before concatenation : %s",string1);
  strcat(string1,string2);
  printf("\nString-1 after concatenation : %s",string1);
}  

 

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

#include<stdio.h>  
#include<string.h>

int main()  
{  
  char string1[]="Coding Ninjas!";
  char ch='i';

  char *val=strchr(string1,ch);
  printf("The string after first occurrence of %c in the string %s : %s",ch,string1,val);
}  

 

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

#include<stdio.h>  
#include<string.h>

int main()  
{  
  char string1[]="Coding Ninjas!";
  char ch='x';

  char *val=strchr(string1,ch);
  printf("The string after first occurrence of %c in the string %s is %s",ch,string1,val);
}  

 

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

#include<stdio.h>  
#include<string.h>

int main()  
{  
  char string1[]="Coding Ninjas!";
  char ch='j';

  char *val=strrchr(string1,ch);
  printf("The string after last occurrence of %c in the string %s is %s",ch,string1,val);
}  

 

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

#include<stdio.h>  
#include<string.h>

int main()  
{  
  char string1[]="Coding Ninjas!";
  char ch='x';

  char *val=strrchr(string1,ch);
  printf("The string after last occurrence of %c in the string %s is %s",ch,string1,val);
}  

 

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

#include<stdio.h>  
#include<string.h>

int main()  
{ 
  char string1[]="Ninja";
  char string2[]="Ninja";

  int val=strcmp(string1,string2);
  printf("On comparing %s and %s the value returned is %d",string1,string2,val);
}

 

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

#include<stdio.h>  
#include<string.h>

int main()  
{ 
  char string1[]="ninja";
  char string2[]="Ninja";

  int val=strcmp(string1,string2);
  printf("On comparing %s and %s the value returned is %d",string1,string2,val);
}

 

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

#include<stdio.h>  
#include<string.h>

int main()  
{ 
  char string1[]="Ninja";
  char string2[]="ninja";

  int val=strcmp(string1,string2);
  printf("On comparing %s and %s the value returned is %d",string1,string2,val);
}

 

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

#include<stdio.h>  
#include<string.h>

int main()  
{ 
  char string1[]="CodingStudio";

  int len=strlen(string1);
  printf("Length of %s is %d",string1,len);
}

 

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

#include<stdio.h>  
#include<string.h>

int main()  
{ 
  char string1[]="Hello";
  char string2[]="World";

  printf("String-1 before strcpy() function : %s",string1);
  strcpy(string1,string2);
  printf("\nString-1 after strcpy() function : %s",string1);

}

 

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

#include<stdio.h>  
#include<string.h>

int main()  
{  
  char string1[]="Hi Coders,All the best!!!";
  char string2[]="Coders";

  char *val=strstr(string1,string2);
  printf("The string after the first occurrence of %s in %s is : %s",string2,string1,val);
}  

 

Output

The string after the first occurrence of Coders in Hi Coders,All the best!!! is : Coders,All the best!!!

Few More functions

  1. strncat() : This function is used to append only the first n characters of a string at the end of another. 
  2. strncpy() : This function is used to copy only the first n characters of one string into another.
  3. strncmp() : This function compares only the first n characters of the two strings.
  4. strcmpi() : This function compares two strings without regard to the case.
  5. strnicmp() : This function is similar to strncmp(), as it is used to compare only the first n characters of a string, but it is not case-sensitive.  
  6. strdup() : This function is used to duplicate a string.
  7. strset() : This is used to set all the characters of a string with a particular given character.
  8. strnset()  : This function sets only the first n characters of a string with a particular given character.
  9. strrev() : This function is used to reverse a string.

 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Frequently Asked Questions

Whats the difference between strchr() and strrchr()?

The strchr() and strrchr() both take two arguments: a string and a character. The strchr() function is used to find the first occurrence of the character in the string, whereas the strrchr() function is used to find the last occurrence of the character in the string.

Is null character included in the length calculated by strlen()?

No, the null character is not included in the length calculated by strlen().

What is the function of strset()?

The strset() function is used to set all the characters of a string to a given character.

Conclusion

We hope you have gained some insights on String Manipulation in C through this article. We hope this will help you excel in your preparation and enhance your knowledge of String Manipulation in C and related stuff. 

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Character Array in C
Next article
For Loop in C
Live masterclass