Most Popular String Functions in C [with Examples]

Updated on July 24, 2024

Article Outline

In every programming language, String manipulation is a fundamental aspect of programming in C, especially when dealing with text-based data. In C language, there are many rich sets of functions for working with strings, allowing developers to perform various operations such as copying, concatenation, comparison, searching, and tokenisation. In this article, we will explore some of the most commonly used stings functions in C, along with examples of demonstrating their usage.

What is String in C?

In C language, A string is a sequence of characters stored in a Contiguous memory location. It means a string is represented as an array of characters terminated by a null character ‘0’. This null character that marks the end of the string is used to denote the end of the string.

 

Here is the syntax of String in C language

char str[] = "Hello";
*Image
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure

Introduction to C String Functions

Developers are often required to modify strings and perform several operations on them according to our needs. If developers want to get the length of the string, there is a length method in the C language. The string handling functions are defined in the header file string.h. This header file must be included in the C program to use the string handling functions.

String Declaration

There are two ways to declare strings in the C language. Let’s discuss both string techniques.

 

  • In this example, we will create a string called “Murli.” The last character must always be a null character. The size mentioned within the brackets is the maximum number of characters a string can hold.

 

For example

char name[5] = {'M', 'U', 'R', 'L', 'I',  '0'};
  • In this method, we will not need to put the null character at the end of the string constant. C compiler automatically inserts the null character at the end of the string.
char name[] = "Murli";

String Functions

The following are the string functions in C language.

 

Function Description
strlen() This function returns the length of the string
strlen() This function returns the string length. It does not count the null character ‘0’
strcmp() It compares two strings and returns 0 if the strings are the same.
strncmp() It compares two strings and returns 0 if the strings are the same
strcat() It concatenates two strings and returns the concatenated string.
strncat() It concatenates n character
strncpy() It copies the first n characters of one string into another string.
strrchr() It finds out the last occurrence of a given character in a string.

 

strstr() It finds out the first occurrence of a given string in a string
strnstr() It finds out the first occurrence of a given string in a string where the search is limited to n characters.
strcasecmp() It compares two strings without case sensitivity to the case
strncasecmp() It compares n characters of one string to another without case sensitivity to the case.

Examples of String Function in C

Let’s understand commonly used string functions in C language.

strlen()

The following program demonstrates the strlen() function. We will find the length of the string using strlen() function.

 

Program

#include <stdio.h> #include <string.h>  int main() { char str[] = "Hello, World!"; int length = strlen(str); printf("Length of the string: %dn", length); return 0; }

Output

Length of the string: 13

strnlen()

The strlen() function takes a string and a positive integer and returns the length of the string if maxlen is greater than the string’s size. It counts characters until str[maxlen-1].

 

The following program demonstrates strnlen() function.

 

Program

#include <stdio.h> #include <string.h>  int main() {  char string1[50] = "Hermoine Granger"; printf("Length of the string: %ld n", strnlen(string1, 25));  }

Output

Length of the string: 16

strcmp()

The strcmp() function compares two inputs as a string. Then, it returns an integer value based on the following conditions:

 

Expression Returns
string 1 > string 2 Positive integer
string 1 < string 2 Negative
string 1 = string 2 zero

 

The following program demonstrates the strcmp() function.

 

Progarm

#include <stdio.h> #include <string.h>  int main() {  char s1[20] = "Murli"; char s2[20] = "Darling";  // comparing both the strings if (strcmp(s1, s2) == 0) { printf("string 1 and string 2 are equal"); } else { printf("string 1 and 2 are different"); } }

Output

string 1 and 2 are different

strncmp()

The strncmp() function compares only the first n characters of both the strings and returns the integer.

 

Expression Returns
String 1(first n characters) > string 2 (first n characters) Positive Integer
String 1 (first n characters) < string 2 (first n characters) Negative
String 1 (first n characters) = string2 (first n characters) Zero

 

Program

#include <stdio.h> #include <string.h>  int main() {  char s1[20] = "neeraj kumar"; char s2[20] = "Neeraj Kumar"; if (strncmp(s1, s2, 5) == 0) { printf("string 1 and string 2 are equal"); } else { printf("string 1 and 2 are different"); } }

Output

string 1 and 2 are different

strcat()

The strcat() function concatenates two strings, the first and the second, which means it attaches the second string to the end of the first string and saves the concatenated string to the first string.

 

The following program demonstrates the strcat() function:

 

Program

#include <stdio.h> #include <string.h>  int main() {  char string1[10] = "Harry "; char string2[10] = "Potter"; strcat(string1, string2); printf("Output string after concatenation: %s", string1);  }


Output

Output string after concatenation: Harry Potter

strncat()

The strncat() function takes two strings as input and attaches them. This function only attaches the first n characters of the string in the second string.

 

The following program demonstrates the strncat() function:

 

Progarm

#include <stdio.h> #include <string.h>  int main () {  char string1[10] = "Hello"; char string2[10] = "World"; strncat(string1,string2, 3); printf("Concatenation using strncat: %s", string1);  }

Output

Concatenation using strncat: HelloWord

strcpy()

The strcpy() function takes two strings as input and overwrites the data of the second string into the first string. The following program demonstrates the strcpy() function.

 

Program

#include <stdio.h> #include <string.h>  int main() {  char s1[35] = "Neeraj "; // string1 char s2[35] = "Harry Potter"; // string2 strcpy(s1, s2); printf("String s1 is: %s", s1);  }

Output

String s1 is: Harry Potter

strncpy()

The strncpy() function takes two string inputs and overwrites the data of the first string with a second string based on a specific condition. The following program demonstrates the strncpy().

 

Program

#include <stdio.h> #include <string.h>  int main() {  char string1[30] = "string 1"; char string2[40] = "Only 12 characters will be copied."; strncpy(string1, string2, 12); printf("String s1 is: %s", string1); }

Output

String s1 is: Only 12 char

strchr()

The strchr() function takes a character as input and finds the first occurrence of the given character in that string. It will return the pointer to the first occurrence of that character. If it does not find anything, it will return NULL.

 

Program 1

#include <stdio.h> #include <string.h>  int main() {  char string1[50] = "I am a software engineer"; printf("%s", strchr(string1, 's')); }

Output

software engineer

Program 2: When the character is not present in the given string, it returns a null value.

#include <stdio.h> #include <string.h>  int main() {  char string1[30] = "Neeraj Kumar "; printf("%s", strrchr(string1, 'z')); }

Output

(null)

strstr()

It takes two strings as input and finds the first occurrence of the second in the first string. This function will return a pointer to the start of the first occurrence of the second string in the first string and a NULL. if the second string is not present in the first string.

 

Program

#include <stdio.h> #include <string.h>  int main() {  char string1[70] = "You are reading string functions."; printf("The Output string is: %s", strstr(string1, "string")); }

Output

The output string is a string function.

strcasecmp()

The strcasecmp() function takes two inputs and compares them irrespective of their case sensitivity.

 

if Returns
str1  < str2 Less than 0
str1 = str2(ignoring case) 0
 str1 > str2 Greater than  0

 

Program

#include <stdio.h> #include <string.h>  int main () {  char string1[70] = "NEERAJ"; char string2[70]= "Neeraj"; int result;  result = strcasecmp(string1, string2);  if (result == 0) printf("Strings are equal.n");  else if (result < 0) printf(""%s" is less than "%s".n", string1, string2);  else printf(""%s" is greater than "%s".n", string1, string2); }

Output

Strings are equal.

strncasecmp()

The strncasecmp() function takes two parameters that are strings. It compares them to n characters irrespective of their case case sensitivity.

 

The following program strncasecmp() function:

 

Program

#include <stdio.h> #include <string.h>  int main() {  char string1[70] = "Working"; char string2[70] = "working"; int result;  result = strncasecmp(string1, string2, 3);  if (result == 0) printf("Strings are equal.n");  else if (result < 0) printf(""%s" is less than "%s".n", string1, string2);  else printf(""%s" is greater than "%s".n", string1, string2); }

Output

Strings are equal.

Conclusion

In this article, we learned about string manipulation in the C language, including understanding and using a set of fundamental string functions provided by the C standard library. We learned the most commonly used standard functions, such as strlen (), strcpy (), strcat (), strcmp (), strchr (), and strstr (). These functions enable tasks such as determining strings. By familiarising yourself with these functions and practising their usage through examples, the developer can effectively manipulate strings using those functions.

FAQs
The strlen() function is used for determining the length of the string. It returns the integer value excluding the null character.
Yes, ‘strchr()’ searches for the first occurrence of a character in a string. It returns a pointer to the first occurrence of the character or NULL if the character is not found.
In the C language, most strings, such as strcmp(), strchr(), and strstr(), are case-sensitive. This means they differentiate between uppercase and lowercase characters.
No, C language string functions cannot directly modify the contents of the original string. Instead, the result is returned as a new copy of the string.

Updated on July 24, 2024

Link
left dot patternright dot pattern

Programs tailored for your success

Popular

Management

Data Science

Finance

Technology

Future Tech

Upskill with expert articles

View all
Hero Vired logo
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.
Blogs
Reviews
Events
In the News
About Us
Contact us
Learning Hub
18003093939     ·     hello@herovired.com     ·    Whatsapp
Privacy policy and Terms of use

|

Sitemap

© 2024 Hero Vired. All rights reserved