I have written code to Remove spaces from a string but it doesn't work...
Code:stringspace string; for (int i = 0; i <str. length; i + +) ( if (str [i]! = "") stringspace + = str [i]; )
I have written code to Remove spaces from a string but it doesn't work...
Code:stringspace string; for (int i = 0; i <str. length; i + +) ( if (str [i]! = "") stringspace + = str [i]; )
Try the following code to Remove All Spaces from a String :
Code:void delspace (char *Str) { int Ptr = 0; int Dst = 0; while (Str [Ptr]) { if (Str [Ptr] != ' ') Str [Dst++] = Str [Ptr]; Ptr++; } Str [Ptr] = 0; }
Copy all the characters of this string to another string variable. While doing so, don't add the space charactes (space, or tab). ispunct()- finds the punctuation character. isspace()-finds the whitespace character.Check whether the current character is a space.That is, Scan the string character by character. If yes, then continue. If no, then copy this to the new variable. At the end of scanning, you have the string without character in the new string variable.
Code:#include <stdio.h> #include <ctype.h> #include <conio.h> void main() { char cspace[80] = "Where there is will,there is a way."; char *q1 = cspace; char *q2 = cspace; q1 = cspace; while(*q1 != 0) { if(ispunct(*q1) || isspace(*q1)) { ++q1; } else *q2++ = *q1++; } *q2 = 0; printf("\nAfter removing the spaces,string is:%s\n", cspace); getch(); }
Bookmarks