In this article we will learn about String Manipulation in C Programming. We will see different examples on manipulating strings.
As we have already seen in the previously, a string is a collection of characters and strings are maintained as character arrays in C programs.
The common operations or manipulations that can be performed on strings are:
- Reading and writing strings
- Concatenating/combining/joining strings
- Comparing strings
- Copying one string into another string
- Extracting a portion of the string (substring)
C provides predefined functions for performing all the above operations or manipulations on strings. Most of these predefined functions are available in string.h header file. The list of predefined functions is given below:
Functions | Purpose |
scanf, gets, getchar | To read a string |
printf, puts, putchar | To print a string |
strcat | To concatenate two strings |
strcmp | To compare two strings |
strcpy | To copy one string into another string |
strstr | To locate a substring in the string |
strlen | To calculate the length of the string |
strrev | To reverse the given string |
Note: The difference between scanf and gets is, scanf can read strings which does not contain any white spaces. Whereas gets can read strings both without spaces and with spaces.
Contents
strcat( )
The strcat predefined function is used to concatenate/join two strings together. The syntax of the strcat function is shown below:
The strcat function accepts two parameters which are strings. The string2 parameter can be either a character array or a string constant. The strcat function takes the content of string2 and merges it with the content in string1 and the final result will be stored in string1. Let us see an example:
strcmp( )
The strcmp predefined function is used to compare two strings. After comparison, if the two strings are equal, then the function returns a 0. Otherwise if the first string comes before the second string in alphabetical order, the function returns a -1. If the first string comes after the second string in alphabetical order, the function returns a 1 as the return value.
The syntax of strcmp function is as shown below:
Let us consider an example:
As seen from the above example, the strcmp function the first letter in both the strings and since they are equal, now it compares the second letter in both the strings, which are e and a. Since, e comes after a according to dictionary order, the result will be 1 which means, the string hello comes after (greater than) the string hai.
strcpy( )
The strcpy function is used to copy one string into another string. This function can be used for creating a copy of an existing string. The syntax of strcpy function is as shown below:
In the above syntax, the string2 can be either a string or string constant. The string in string2 is copied into string1 and the result will be stored in string1. Let us consider an example:
strlen( )
The strlen function is used to retrieve the length of a given string. The return type of this function will be an integer. The syntax of strlen function is as shown below:
The parameter string can be either a character array or a string constant. The function returns the length of the string which will be the number of characters in the string excluding the ‘\0’ character. Let us consider an example:
strtstr( )
The strstr function returns a character pointer of the first occurrence of the given substring in the string. If the substring is not found in the string, the function strstr returns NULL. The syntax for strstr function is shown below:
In the above syntax, strstr searches for string2 in the string1. If found, it returns a pointer to the first occurrence of the string2 in string1. If not found, it returns NULL. Let us consider an example:
strrev( )
The strrev function is used to reverse a given string. We can use this predefined function to check whether a given string is a palindrome or not. The syntax for using the strrev function is as shown below:
In the above syntax, the strrev function reverses the given string and returns it back. The content of the string also changes. Let us see an example:
Other predefined functions
Some of the other predefined functions available in the string.h header file are shown in the below table:
Function | Purpose | Syntax |
strncat | To concatenate n number of left most characters | strncat(str1, str2, n) |
strncmp | To compare n number of left most characters | strncmp(str1, str2, n) |
strncpy | To copy n number of left most characters | strncpy(str1, str2, n) |
Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.
He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.
He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.
I blog often and I genuinely thank you for your information. The article has
truly peaked my interest. I will bookmark your site and
keep checking for new information about once per week. I subscribed
to your RSS feed too.