Beginners Programs :

C Program To Trim Leading & Trailing White Space Characters From String

C program to trim both leading and trailing whitespace characters from a given string – In this article, we will brief in on the several ways to trim both leading and trailing whitespace characters from a given string in C programming.

Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.

The ways used in this piece are as follows:

  • Using Standard Method
  • Using Function

A string is nothing but an array of characters. The value of a string is determined by the terminating character. Its value is considered to be 0.

As you can see with this example, firstly, we need to enter the respective string.

The string, in this case, is ”   Removing leading and trailing white spaces   “

Hence, the quest is to remove the leading and trailing white spaces.

So, the string afterwards becomes “Removing leading and trailing white spaces”

Thus, the means explained in this piece to do the same in C programming are as follows:

Using Standard Method

  1. Read the user entered string and store in the variable ‘s’ using gets(s) function.

2) Print the string before trimming leading and trailing white spaces.

3) a) To remove the leading white spaces

Using 1st for loop increase the index value of the string until it reaches the first nonwhite-space character of the string. So the value of i at the last iteration indicates the index of the first non white-space character of the string.

Using 2nd for loop arrange the characters after removing leading whitespaces, from the zero indexes in the string s[].initialize the last element with a null character.

b) To remove trailing white spaces

The for loop iterates through the string until the last character of the string becomes to null with the structure for(i=0;s[i]!=’\0′;i++).

If the element of the string s[i] not equal to white space and a tab then only initialize j with the value of i. Repeat this step until the last character of the string becomes to null. Here j indicates the index of the last nonwhite-space character of the string.

After iterations of the for loop initialize end of the string with the null character as s[j+1]=’\0′.

4) Then print the string after removing the leading and trailing white spaces.

Output:

Using Function

  1. The main() calls the trimleadingandTrailing(char *s) to remove the leading and trailing whitespaces.

2) The function has both the codes

a) To remove leading white spaces–Using 1st for loop it increases the index of the string until it reaches the 1st non-whitespace character of the string. After removing the leading white spaces, Using 2nd for loop place the elements of the string from the zero index.

b) To remove trailing whitespace–Iterate the for loop through the string until the last character of the string becomes to null. Check each element of the string, if the element of the string is not equal to white space and a tab then initialize j=i. Repeat this step until the null character.

j is the index of the last nonwhite-space character of the string. Initialize the last element of the string with null as s[j+1]=’\0′.

3) Print the string after removing leading and trailing white spaces.

Output: