All String Functions in C | C String Library Functions

You are currently viewing All String Functions in C | C String Library Functions

In this article we will learn about all string functions in C programming language.

Before going into the details about string functions in C, we need to learn different ways to take string from user and print it.

The most common way to print a string is using printf() function. printf() function prints the string which is passed in it. Consider the following example.

#include <stdio.h>
int main(){

  printf("I am learning how to print string in C\n");
  
  return 0;
}

This will print the string inside it which is “I am learning how to print string using C”.

But we can also print string using character array and printf() function. %s is used as the format specifier for printing a string.

#include <stdio.h>
int main(){
  char char_array[50] = "I am learning to print string here.";

  printf("%s\n", char_array);

  return 0;
}

Output of this print string program is as follows;

print-string-program-in-c

Taking string from the user and print string

We can take sting input from the user using scanf() and gets() function. These two functions are defined inside stdio.h header file.

#include <stdio.h>
int main(){
    char my_string[50];

    printf("Enter any string here to print it : \n");
    scanf("%s", my_string);   // not use &my_string in case of string

    printf("\nYou have entered '%s'.\n", my_string);

    return 0;
}

The problem here is that we can not take space separated string as input in case of using scanf() function. See the output bellow.

Enter any string here to print it :
competitive programmer

You have entered 'competitive'.

To overcome this problem we can use gets() function to take string input. It will read the whole string with space.

#include <stdio.h>
int main(){
  char given_string[50];

  printf("Enter any space separated string here : \n");
  gets(given_string);

  printf("\nYou have entered the following string : \n%s\n", given_string);
  
  return 0;
}

Output :

Enter any space separated string here :
competitive programmer is best for C

You have entered the following string :
competitive programmer is best for C

Print string using loop in C

We can also print a string using any loop in C although we will use while loop here.

The logic will same for every loop. We will determine the end of the string by NULL character.

// print string using while loop

#include <stdio.h>

int main(){
   char char_str[50];
   int r = 0;

   printf("Enter any string here : \n");
   gets(char_str);

   printf("\nYou have entered : \n");
   
   while (char_str[r] != '\0'){
      printf("%c", char_str[r]);
      r++;
   }

   return 0;
}

Output of this print string program is as follows;

Enter any string here :
I can print string with C now.

You have entered :
I can print string with C now.

After learning the basics of string that how to print and take string from the user, we will see all string functions here.

All string functions in C

There are some very useful and efficient string functions in C language. We will discuss about all of theme in this article.

The list of string functions which we will discuss is given bellow;

  • puts()
  • gets()
  • strlen()
  • strset()
  • strnset()
  • strchr()
  • strrchr()
  • strcmp()
  • strncmp()
  • strcpy()
  • strncpy()
  • strdup()
  • strlwr()
  • strupr()
  • strrev()
  • strcat()
  • strncat()
  • strstr()

Now let’s see the details of each string functions in C with their use inside an example program.

Puts string functions in C

Puts string functions is used to print string. Here is an example C program using puts string function.

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

int main () {
   char str[20];

   strcpy(str, "worldtechjournal");

   puts(str);
   
   return(0);
}

Output of this puts string functions program:

worldtechjournal

Gets string functions in C

Gets string functions is used to take user input. The syntax and use of gets string functions is given bellow.

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

int main () {
   char str[40];

   printf("Enter any string : ");
   gets(str);

   printf("You have entered: %s", str);

   return(0);
}

See the output like this:

Enter any string : worldtechjournal.com
You have entered: worldtechjournal.com

Strlen string library functions in C

The strlen() function can determine the length of a string and return the length of given string.

It returns the length without counting the null character. We use strlen() function to determine the length of any string.

Let’s consider the following example C program to understand about strlen() function.

// strlen() function in C, length of a string
#include<stdio.h>
#include <string.h>
int main(){
  char ch[50]={'C', 'o', 'm', 'p', 'e', 't', 'i', 't', 'i', 'v', 'e', '\0'};
    printf("Length of given string is = %d\n", strlen(ch));
  return 0;
}

Output of this program is like this:

strlen() string functions in c

Strset library functions in C

The strset() function sets all the characters of a string to the given character we want to set.

It is a non standard function and may not exist in standard library in C. See the syntax of strset() here.

// syntax of strset() string function in C

char *strset(char *String, int c);

Now, lets see a C program using strset() function.

// strset() function in C
#include<stdio.h>
#include<string.h>
int main(){
   char mainStr[20] = "Programmer";

   printf("Original string before strset() is : \n%s\n\n", mainStr);

   printf("After using strset() : \n%s\n",strset(mainStr,'p'));

   return 0;
}

Program output:

strset library functions in c

Strnset string functions in C

strnset() is also a built in string function in C which sets the first n characters of string to a specific character.

But when n is greater than the length of string, then the length of string will be used instead of n. Syntax of strnset() function is given here.

// syntax of strnset() string function in C

char *strnset(const char *string, char ch, int n);

The program bellow will help you to know more about strnset() function in C.

// strnset() function in C
#include <stdio.h>
#include <string.h>

int main(){
  char myStr[] = "competitiveprogrammer";

  printf("Original String is : \n%s\n\n", myStr);

  printf("String after using strnset is : \n%s\n", strnset(myStr, '#', 5));

  return 0;
}

This program will give you the output like this:

strnset string functions in c

Strchr string functions in C

strchr() function is used to search a character inside a string. It gives another string containing the characters after the matched character of the first string. Syntax of strchr() is given here.

// syntax of strchr() string function

char *strchr(char *String, int ch);

Here we have used char with data type int because strchr() will convert the character to integer for better searching when we use char inside the strchr() function.

Now, consider the following example for better understanding.

// strchr() function in C
#include <stdio.h>
#include <string.h>
int main(){
  char myString[50] = "I am learning from competitiveprogrammer.com";

  printf ("%s\n", strchr(myString, 'g'));   // search for char g

  return 0;
}

Output is given bellow:

C strchr() and strrchr() function, strchr() function in C

Strrchr library functions in C

strrchr() string function is also used to search a character inside a string like strchr() function, but it searches the character inside the string in reverse order.

Syntax of strrchr() string function is given bellow.

// syntax of strrchr() string function

char *strrchr(char *mainString, int ch);

Now, lets see an example of C program using strrchr() string function.

// strrchr() function in C
#include <stdio.h>
#include <string.h>
int main(){
  char myString[50] = "worldtechjournal.com is best for learn C";

  printf ("%s", strrchr(myString, '.'));

  return 0;
}

You will see the output like this:

strrchr() function in C

Strcmp string functions in C

strncmp() function is used to compare first n characters of two string. It compares only the first n characters. Syntax of strncmp() string functions is given bellow.

// syntax of strncmp() string function

int strncmp(const char *firstString, const char *secondString, size n);

Example of C program using strncmp() function.

// strncmp() function in C
#include <stdio.h>
#include <string.h>
int main(){
  char firstString[30] = "Competitive";
  char secondString[30] = "Competitive programmer";

  if (strncmp(firstString, secondString, 11) == 0){   // checking first 11 characters
    printf("string 1 and string 2 are equal");

  }else{
    printf("string 1 and 2 are different");
  }

return 0;
}

Program output:

strncmp() function in C

Strncmp string functions in C

We can compare two string using strcmp() string function. The strcmp() function returns 0 if both of the string is equal otherwise it returns positive or negative integers.

Syntax of strcmp() function is as follows.

// syntax of strcmp() function in C

strcmp(firstString, secondString);

The strcmp() function will return negative integer if second string is bigger than first.

On the other hand it will return a positive integer if first string is bigger than second. Now, lets see a C program to compare two string.

// program using strcmp() function in C, Compare string
#include<stdio.h>
#include <string.h>
int main(){
  char firstString[20],secondString[20];

  printf("Enter first string here : ");
  gets(firstString);
  printf("Enter second string here : ");
  gets(secondString);

  if(strcmp(firstString,secondString)==0){
      printf("\nBoth strings are equal.\n");

  }else if(strcmp(firstString,secondString) > 0){
      printf("\nFirst string is bigger.\n");

  }else{
      printf("\nSecond string is bigger.\n");
  }
 return 0;
}

Output of this compare string function is given bellow:

compare two string using strcmp() string function

Strcpy library functions in C

We use strcpy() function to copy a string from any source to destination.

At first we should know the syntax of strcpy() function. Here is the syntax of strcpy() function in C.

// syntax of strcpy() function

strcpy(destination, source);

Let’s see a C program of strcpy() function in C.

// strcpy() function in C, copy a string
#include<stdio.h>
#include <string.h>
int main(){
  char firstArray[20]={'C', 'o', 'm', 'p', 'e', 't', 'i', 't', 'i', 'v', 'e', '\0'};
  char secondArray[20];

  strcpy(secondArray, firstArray);

  printf("First string is : %s\n",firstArray);
  printf("Second string is : %s\n",secondArray);

return 0;
}

Here is the output of above program:

strcpy() function in C, copy a string

Strncpy library functions in C

strncpy() function is used to copy a string to another as like as strcpy() string function.

But the difference is that in strcpy() we can copy the entire string to another and here in strncpy() we can copy the required number of characters from the string.

Then these characters will replace the first n characters from the first string. The syntax of strncpy() function is like as following.

// syntax of strncpy() function

char *strncpy( char *firstString, char *secondString, size n);

It should be mentioned that if the number of characters n is less than the number of characters in second string then it will copy first n characters from second string to first.

But if the number of characters n is greater than the number of characters of second string then strncpy() function will copy entire second string to the first with the required number of null characters need to fill out taken number n.

Let’s see an example C program using strncpy() function.

// strncpy() function in C
#include <stdio.h>
#include <string.h>
int main(){
  char firstString[30] = "competitive programmer";
  char secondString[30] = "C programming tutorials";

  strncpy(firstString,secondString, 11);    // replace first 11 char from firstString

  printf("First string is : %s\n", firstString);
  return 0;
}

Output of strncpy() function program

strncpy() function in C

Strdup string functions in C

The strdup() string function is used to duplicate a string. See the example bellow.

// strdup() function in C
#include<stdio.h>
#include<string.h>
int main(){
    char mainString[] = "Competitive programmer\n";

    char* copiedString = strdup(mainString);

    printf("Main string is : %s\n", mainString);
    printf("Copied string is : %s\n", copiedString);

    return 0;
}

The program will give the following output if you compile and run it.

strdup() function in C

Strlwr string library functions in C

The strlwr() function returns the same string converting all the characters in lowercase.

We use this function when we need to make an entire string in lowercase.

Here is the C program to make a lowercase string using strlwr() function.

// strlwr() function in C, lowercase string
#include<stdio.h>
#include <string.h>
int main(){
  char yourString[30];

  printf("Enter a string to make it lowercase : ");
  gets(yourString);

  printf("\nYou have entered : %s\n",yourString);
  printf("Lowercase String is : %s\n",strlwr(yourString));

 return 0;
}

Output of this string function is as follows;

strlwr() function in C, lowercase string

Strupr string functions in C

strupr() function converts whole string to uppercase and return string characters in uppercase. We use strupr() to convert all the character in uppercase. See the C program bellow.

// strupr() function in C, uppercase string
#include<stdio.h>
#include <string.h>
int main(){
  char myString[20];
  printf("Enter a string to convert : ");
  
  gets(myString);
  
  printf("\nsYou have entered string : %s",myString);
  printf("\nString in uppercase is : %s\n",strupr(myString));
 return 0;
}

Output:

strupr() function in C, uppercase string

Strrev string functions in C

strrev() function can reverse a string. This string function is used to reverse our given string and it returns the reverse string.

See the C program bellow which we have used strrev() function.

// strrev() function in C, reverse string
#include<stdio.h>
#include <string.h>
int main(){
  char strg[30];

  printf("Enter a string to reverse it : ");
  gets(strg);

  printf("You have entered : %s\n",strg);

  printf("\nReverse String is : %s\n",strrev(strg));

return 0;
}

Output of this reverse string program is as follows:

Enter a string to reverse it : competitive
You have entered : competitive

Reverse String is : evititepmoc

Strcat string functions in C

We use strcat() string function to concatenate two string. It will concatenate second string after first string. Syntax of strcat() function is as follow.

// Syntax of strcat() function

strcat(firstString, secondString);

Now, we will see an example program using strcat() function.

// c program using strcat() string function
#include<stdio.h>
#include <string.h>
int main(){
  char firstStr[10]={'H', 'e', 'l', 'l', 'o', '\0'};
  char secondStr[10]={'W', 'o', 'r', 'l', 'd', '\0'};

  printf("First string before concatenate : %s\n",firstStr);

  strcat(firstStr,secondStr);
  printf("\nFirst string after concatenate : %s\n",firstStr);

  return 0;
}

Output of this C program :

strcat() function in C, concatenate string

Strncat library functions in C

strncat() is also a concatenate string function like strcat(). It concatenate n characters of second string to the first string.

The null character will always be added to the end of the string. The syntax of strncat() string function is given bellow.

// syntax of strncat() string function

char *strncat(char *firstString, char *secondString, int n);

Let’s see the program bellow to see the use of strncat() function in C.

// strncat() function in C
#include <stdio.h>
#include <string.h>
int main(){
  char firstStr[20] = "Competitive";
  char secondStr[20] = "Programmer";

  strncat(firstStr, secondStr, 3);

  printf("First string after concatenating is : \n%s\n", firstStr);
  return 0;
}

Output:

strncat() function in C

Strstr string functions in C

strstr() function returns another string which is sub string of the main string. It returns the pointer to the first occurrence of matched string till the last character.

strstr() takes two parameters to perform its function. One is the main string where it needs to search and another is the matched content. See the syntax of strstr() function here.

// syntax of strstr() function

char *strstr(const char *string, const char *matched);

Consider the following program where we have used the strstr() function to print a sub string from the main string.

// strstr() function in C
#include<stdio.h>
#include <string.h>
int main(){
  char mainString[100]="I love C programming with competitive programmer";
  printf("Main string is : \n%s\n", mainString);

  char *subString;

  subString = strstr(mainString,"with");

  printf("\nSubstring is : \n%s\n",subString);

 return 0;
}

Output:

strstr() function in C, making a substring

I hope you are now clear about the topics of all string functions in C. If you have any queries or opinion then feel free to share bellow.

Leave a Reply