In this program, we are going to share a c program remove spaces, blanks from a string. If you are a beginner and want to start learning the c programming, then keep your close attention in this tutorial as I am going to share a c program remove spaces, blanks from a string with the output.
Copy below c program and execute it with c compiler to see the output of the program.
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 |
#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; } |
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 |
#include <stdio.h> #include <string.h> #include <stdlib.h> #define SPACE ' ' 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) { int length, c, d; char *start; 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; } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.