Remove all whitespaces from String in C Language
Remove All Whitespaces from String in C Program Description:
Write a Program to Remove All Whitespaces from String in C programming language. The program should accept a string from the user and string may contain leading, Trailing, and even the whitespace between the words. The program should remove all whitespace(unnecessary whitespaces) from the string and return the resultant string.
Example Input and Output:
Input:
Enter a string : Making Mistakes means you're learning fast!
Output:
1 2 |
Before Removing Whitespaces: Making Mistakes means youre learning fast! (Actual string) After Removing All Whitespaces:Making Mistakes means youre learning fast!(Resultant string) |
In the above example, The input string has leading, and trailing whitespaces and even there are a few extra whitespaces in between the string. The program should remove all whitespaces and return the resultant string. ( The resultant string will contain one whitespace character between the characters)
Program Prerequisites:
We are going to use the C-String, C-Arrays, and Functions in C in the following program. It is good idea to know the basics of these concepts to understand the program better.
- Strings in C Language – Declare, Initialize, and Modify Strings
- Arrays in C with Example Programs
- Functions in C – How to Create and Use functions in C
Remove All Whitespaces from String in C Program Explanation:
As we have specified earlier, The input string might contain Leading whitespaces, Trailing whitespace, and the whitespaces in between the words. So we need to handle all the above cases and remove all whitespaces.
We have already looked at the programs to
- Remove Leading Whitespaces from a String
- Remove Trailing whitespaces from a String
- Remove extra whitespaces in between words in a String/Sentence.
We are going to utilize the above three programs to remove all whitespace characters from a string/sentence.
In the following program, We have defined three user-defined functions. They are trimLeadingWSpaces() function, trimTrailingWSpaces() function, and removeWSpacesBWWords() function. Let’s look at the details of each function.
The trimLeadingWSpaces() function:
- Function Prototype: void trimLeadingWSpaces(char * str)
- This function is used to remove the Leading whitespaces from the given string( str).
- To Remove the Leading whitespace from the string, Shift the valid characters(non-whitespace characters) to the start(Zeroth Index) of the string. Please go through the Remove Leading Whitespaces Program for a detailed explanation.
The trimTrailingWSpaces() function:
- Function Prototype: void trimTrailingWSpaces(char * str)
- This function will remove all trailing whitespaces from the given string str.
- To Remove the Trailing Whitespaces, We will search for the first non-whitespace character in a string in a backward direction, and after we discovered the last non-whitespace character. Then we will update it with the terminating NULL character. Again, Go through the Remove Trailing Whitespaces Program for a detailed step-by-step explanation.
The removeWSpacesBWWords() function:
- Function Prototype: void removeWSpacesBWWords(char *inputStr, char * outputStr)
- This function takes two formal arguments i.e inputStr and outputStr. The removeWSpacesBWWords() function removes all unnecessary whitespaces between the words and stores the resultant string in the outputStr string. Please go through the Remove extra whitespaces in between words in a String program for more details.
- We can also return a string from this function instead of getting the outputStr as argument.
In the main() function, Create two strings called inputStr and outputStr. Take the user input and update the inputStr. Then Call the above three functions one by one to remove all whitespaces from the string.
1 2 3 4 5 6 7 8 9 10 11 |
// Remove the Leading white spaces // call the 'trimLeadingWSpaces' function to remove the leading white spaces trimLeadingWSpaces(inputStr); // Now, Remove the Trailing white spaces // call the 'trimTrailingWSpaces' with 'inputStr' to remove the the Trailing spaces trimTrailingWSpaces(inputStr); // Now, Remove the Whitespaces between words // call the 'removeWSpacesBWWords()' function with 'inputStr' and 'outputStr' strings removeWSpacesBWWords(inputStr, outputStr); |
The first two functions, The trimLeadingWSpaces() and trimTrailingWSpaces() functions changes the inputStr. As we are passing the string by address or reference all the changes made in the functions are visible in the main() function.
Finally, display the resultant string on the console.
Program to Remove All Whitespaces from String in C:
Here is the program to Remove All whitespaces from a String in C Programming Language.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
/* program to Remove All White spaces in a String in C using functions sillycodes.com */ #include<stdio.h> #include<ctype.h> #include<string.h> // Max Size of string #define SIZE 100 /** * @brief - Trim / Remove Leading white spaces in the given string. * * @param str - Input String * @return void */ void trimLeadingWSpaces(char * str) { // Create a variable 'start' and assign 'Zero' int start = 0; int i; // Create a loop and check for leading white spaces while(isspace((unsigned char)str[start])) { // found leading whitespace // increment 'start' start++; } // If there are any leading white spaces, Shift the valid characters to start of the string(Zeroth index). if(start != 0) { // Shift the characters to start of the string. for(i = 0; str[i]; i++) { // Replace element at index 'i' with 'i+start' str[i] = str[i+start]; } // add terminating NULL Character str[i+1] = '\0'; } } /** * @brief - Trim / Remove Trailing white spaces in the given string. * * @param str - Input String * @return void */ void trimTrailingWSpaces(char * str) { int i; // Create a variable 'end' and assign 'len-1' int end = strlen(str) - 1; // Create a loop and check for trailing white spaces while(isspace((unsigned char)str[end])) { // found trailing whitespace // decrement 'end' end--; } // 'end' contains the actual end of the string. // Add terminating NULL Character at 'end+1' str[end+1] = '\0'; } /** * @brief - Remove Whitespaces in between words in given string 'inputStr' * * @param inputStr - Input string * @param outputStr - Output String - Resultant string */ void removeWSpacesBWWords(char *inputStr, char * outputStr) { int i, j; // Iterate over the string - Remove extra spaces between the words // index 'i' used for traversing the inputStr // index 'j' used to update the outputStr for(i=0, j=0; inputStr[i]; i++) { // check if we got the whitespace. if(isspace(inputStr[i]) && isspace(inputStr[i-1])) { // Two consecutive white spaces. Skip the 'i'. Continue to next iteration continue; } // Found a valid character, update outputStr. Increment 'j' outputStr[j++] = inputStr[i]; } // Add the terminating NULL character to outputStr outputStr[j] = '\0'; } int main() { // declare a string with 'SIZE' char inputStr[SIZE]; int i; // Create a Output String char outputStr[SIZE] = {0}; // get the string from the user printf("Enter a string : "); gets(inputStr); printf("Before Removing Whitespaces:%s(Actual string)\n", inputStr); // Remove the Leading white spaces // call the 'trimLeadingWSpaces' function to remove the leading white spaces trimLeadingWSpaces(inputStr); // Now, Remove the Trailing white spaces // call the 'trimTrailingWSpaces' with 'inputStr' to remove the the Trailing spaces trimTrailingWSpaces(inputStr); // Now, Remove the Whitespaces between words // call the 'removeWSpacesBWWords()' function with 'inputStr' and 'outputStr' strings removeWSpacesBWWords(inputStr, outputStr); // Print Results printf("After Removing All Whitespaces:%s(Resultant string)\n", outputStr); return 0; } |
Program Description:
Let’s Compile and Run the Program.
Compile the Program.
1 |
$ gcc remove-all-whitespaces.c |
Run the Program.
Test Case 1: When the input string has Leading Whitespaces
1 2 3 4 5 6 |
// with leading spaces $ ./a.out Enter a string : Houston, We have a Problem Before Removing Whitespaces: Houston, We have a Problem(Actual string) After Removing All Whitespaces:Houston, We have a Problem(Resultant string) $ |
As we can see from the above output, The program removed the reading whitespaces from the input string.
Test Case 2: String with Trailing Whitespaces
1 2 3 4 5 6 |
// trailing spaces $ ./a.out Enter a string : The end is the beginning Before Removing Whitespaces:The end is the beginning (Actual string) After Removing All Whitespaces:The end is the beginning(Resultant string) $ |
In the above example, The input string has trailing whitespaces and the program removed the trailing whitespaces and provided the string without extra spaces.
Test Case 3: String with Whitespaces between words
1 2 3 4 5 6 |
// spaces in between words $ ./a.out Enter a string : Learn Programming Online Before Removing Whitespaces:Learn Programming Online(Actual string) After Removing All Whitespaces:Learn Programming Online(Resultant string) $ |
As we can see from the above output, The input string Learn Programming Online has a few whitespace characters between the words and the program removed all extra whitespaces between the strings and displayed the output string on the console.
Test 4: When the input string contains Leading Whitespaces, Trailing Whitespaces, and whitespaces between the words.
1 2 3 4 5 6 |
// All whitespaces $ ./a.out Enter a string : Making Mistakes means youre learning fast! Before Removing Whitespaces: Making Mistakes means youre learning fast! (Actual string) After Removing All Whitespaces:Making Mistakes means youre learning fast!(Resultant string) $ |
The program removed all whitespaces from a string and returned the resultant string.
Related String Programs:
- C Tutorials Index
- C Programs Index
- C Program to Calculate Length of the string
- C Program to Copy a String to new string
- C Program to Concatenate Two strings
- Count the Number of Vowels, Consonants, Digits, and Special characters in String
- C Program to Count Alphabets, Digits, Whitespaces in String
- C Program to Convert Lower case string to Upper case string
- C Program to Convert Uppercase string to Lowercase string
- C Program to Reverse a String
- C Program to Check Palindrome String
- C Program to Toggle Case of All Characters in String
2 Responses
[…] C Program to Remove All Whitespaces in a String or Sentence […]
[…] C Program to Remove All Whitespaces in a String or Sentence […]