Table of contents
1.
Introduction
2.
String Functions in C
2.1.
strcat()
2.1.1.
Syntax
2.1.2.
Example 
2.2.
C
2.2.1.
Output
2.3.
strchr()
2.3.1.
Syntax
2.3.2.
Example 
2.4.
C
2.4.1.
Output
2.4.2.
Example   
2.5.
C
2.5.1.
Output 
2.6.
strrchr()
2.6.1.
Syntax
2.6.2.
Example 
2.7.
C
2.7.1.
Output
2.7.2.
Example  
2.8.
C
2.8.1.
Output 
2.9.
strcmp()
2.9.1.
Syntax
2.9.2.
A value 0 if both strings are identical 
2.10.
C
2.10.1.
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.11.
C
2.11.1.
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.12.
C
2.13.
strlen()
2.13.1.
Syntax
2.13.2.
Example
2.14.
C
2.14.1.
Output
2.15.
strcpy()
2.15.1.
Syntax
2.15.2.
Example
2.16.
C
2.16.1.
Output
2.17.
strlwr()
2.17.1.
Syntax
2.18.
strupr()
2.18.1.
Syntax
2.19.
strstr()
2.19.1.
Syntax
2.19.2.
Example  
2.20.
C
2.20.1.
Output
3.
Few More functions
4.
String Declaration in C
5.
Advantages of String Functions in C
6.
Disadvantages of String Functions in C
7.
Frequently Asked Questions
7.1.
What is a string function?
7.2.
What are the five types of strings in C?
7.3.
Whats the difference between strchr() and strrchr()?
7.4.
Is null character included in the length calculated by strlen()?
7.5.
What is the function of strset()?
8.
Conclusion
Last Updated: Oct 3, 2024
Easy

String Functions in C

Author Pallavi singh
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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 manipulation in c

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 

  • C

C

#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);
You can also try this code with Online C Compiler
Run Code

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 

  • C

C

#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);
You can also try this code with Online C Compiler
Run Code

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  
 

  • C

C

#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);
}
You can also try this code with Online C Compiler
Run Code

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 

  • C

C

#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);
You can also try this code with Online C Compiler
Run Code

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  

  • C

C

#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);
}
You can also try this code with Online C Compiler
Run Code

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

  • C

C

#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);
}
You can also try this code with Online C Compiler
Run Code

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

  • C

C

#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);
}
You can also try this code with Online C Compiler
Run Code

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

  • C

C

#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);
}
You can also try this code with Online C Compiler
Run Code

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

  • C

C

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

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

int len=strlen(string1);
printf("Length of %s is %d",string1,len);
}
You can also try this code with Online C Compiler
Run Code

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

  • C

C

#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);

}
You can also try this code with Online C Compiler
Run Code

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  

  • C

C

#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);
}
You can also try this code with Online C Compiler
Run Code

Output

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

Few More functions

Function Description
strncat() Appends the first n characters of one string to the end of another.
strncpy() Copies the first n characters of one string into another.
strncmp() Compares the first n characters of two strings.
strcmpi() Compares two strings without regard to case.
strnicmp() Compares the first n characters of two strings in a case-insensitive manner.
strdup() Duplicates a string by allocating memory and copying the string.
strset() Sets all characters of a string to a specified character.
strnset() Sets the first n characters of a string to a specified character.
strrev() Reverses the characters in a string.

String Declaration in C

In C, strings are declared as arrays of characters terminated by a null character ('\0'). This null character marks the end of the string. For example:

char str[] = "Hello, World!";

Here, str is an array of characters that holds the string "Hello, World!" and automatically includes the null terminator. Strings can also be declared as pointers to char:

char *str = "Hello, World!";

In this case, str points to a string literal stored in read-only memory.

Advantages of String Functions in C

  • Efficiency: String functions like strcpy, strcat, and strlen provide optimized and tested ways to handle common string operations efficiently.
  • Consistency: Using standard library functions ensures consistent behavior across different implementations and platforms.
  • Ease of Use: Functions simplify common tasks such as copying, concatenating, and comparing strings, reducing the need for custom code.
  • Memory Management: Functions handle memory allocation and deallocation, reducing the risk of memory-related errors when working with strings.

Disadvantages of String Functions in C

  • Safety Issues: Functions like strcpy and strcat are prone to buffer overflow if not used carefully, leading to potential security vulnerabilities.
  • Lack of Bounds Checking: Many string functions do not check for buffer overflows or invalid access, requiring developers to manually ensure safe usage.
  • Performance Overheads: Some functions may incur performance overhead due to internal checks and operations, especially when dealing with large strings.
  • Limited Flexibility: Functions are designed for specific operations and may not handle all edge cases or complex string manipulations effectively.

Frequently Asked Questions

What is a string function?

A string function is a built-in C library function used to perform operations on strings, such as copying, concatenating, and comparing.

What are the five types of strings in C?

  1. String Literals: Fixed text in double quotes.
  2. Character Arrays: Arrays storing strings.
  3. String Pointers: Pointers pointing to string literals.
  4. Mutable Strings: Strings stored in writable memory.
  5. Constant Strings: Read-only string literals.

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.

Live masterclass