My Blog
Articles related to programming, computer science, technology and research.

22/04/2016 Categories: C Programming. No Comments on String Manipulations

As we have already seen in the previous unit, 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:

  1. Reading and writing strings
  2. Concatenating/combining/joining strings
  3. Comparing strings
  4. Copying one string into another string
  5. 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:

string-functions-c

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.

strcat( )

The strcat predefined function is used to concatenate/join two strings together. The syntax of the strcat function is shown below:

strcat-syn

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:

strcat-ex

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:

strcmp-syn

Let us consider an example:

strcmp-ex

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:

strcpy-syn

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:

strcpy-ex

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:

strlen-syn

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:

strlen-ex

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:

strstr-syn

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:

strstr-ex

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:

strrev-syn

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:

strrev-ex

 

Other predefined functions

Some of the other predefined functions available in the string.h header file are shown in the below table:

other-string-functions

 

Suryateja Pericherla

Suryateja Pericherla

Hello, I am Suryateja Pericherla working as an Asst. Professor in CSE department at Vishnu Institute of Technology. I write articles to share my knowledge and make people knowledgeable regarding certain topics.
Suryateja Pericherla

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me