Show C program to remove spaces or excess blanks from a string, For example, consider the string "C programming"There are two spaces in this string, so our program will print the string C program#include <stdio.h> int main() { char text[1000], blank[1000]; int c = 0, d = 0; printf("Enter some text\n"); gets(text); while (text[c] != '\0') { if (text[c] == ' ') { int temp = c + 1; if (text[temp] != '\0') { while (text[temp] == ' ' && text[temp] != '\0') { if (text[temp] == ' ') { c++; } temp++; } } } blank[d] = text[c]; c++; d++; } blank[d] = '\0'; printf("Text after removing blanks\n%s\n", blank); return 0; } If you want you can copy blank into text string so that original string is modified. Download Remove spaces program. Output of program: C programming code using pointers#include <stdio.h> char *process(char*); int main() { char text[1000], *r; printf("Enter a string\n"); gets(text); r = process(text); printf("\"%s\"\n", r); free(r); return 0; } char *process(char *text) { c = d = 0; length = strlen(text); start = (char*)malloc(length+1); if (start == NULL) exit(EXIT_FAILURE); while (*(text+c) != '\0') { if (*(text+c) == ' ') { int temp = c + 1; if (*(text+temp) != '\0') { while (*(text+temp) == ' ' && *(text+temp) != '\0') { if (*(text+temp) == ' ') { c++; } temp++; } } } *(start+d) = *(text+c); c++; d++; } *(start+d)= '\0'; return start; } Write a C program to remove extra spaces, blanks from a string. How to remove extra blank spaces, blanks from a given string using functions in C programming. Logic to remove extra white space characters from a string in C. Example Input Input string: Learn C programming at Codeforwin.Output String after removing extra blanks: "Learn C programming at Codeforwin"Required knowledgeBasic C programming, Loop, String, Function
Logic to remove extra spaces, blanks
Program to remove extra spaces from string/** * C program to remove extra blank spaces from a given string */ #include <stdio.h> #include <stdlib.h> #define MAX_SIZE 100 // Maximum string size /* Function declaration */ char * removeBlanks(const char * str); int main() { char str[MAX_SIZE]; char * newString; printf("Enter any string: "); gets(str); printf("\nString before removing blanks: \n'%s'", str); newString = removeBlanks(str); printf("\n\nString after removing blanks: \n'%s'", newString); return 0; } /** * Removes extra blank spaces from the given string * and returns a new string with single blank spaces */ char * removeBlanks(const char * str) { int i, j; char * newString; newString = (char *)malloc(MAX_SIZE); i = 0; j = 0; while(str[i] != '\0') { /* If blank space is found */ if(str[i] == ' ') { newString[j] = ' '; j++; /* Skip all consecutive spaces */ while(str[i] == ' ') i++; } newString[j] = str[i]; i++; j++; } // NULL terminate the new string newString[j] = '\0'; return newString; }Enter any string: Learn C programming at Codeforwin. String before removing blanks: 'Learn C programming at Codeforwin.' String after removing blanks: 'Learn C programming at Codeforwin.' Happy coding đ |