how to delete space between two words using c programme... should I use ascii or just compair with ''?
eg. Input- "Hello World" Output-"HelloWorld".
Thanks, Vikram
how to delete space between two words using c programme... should I use ascii or just compair with ''?
eg. Input- "Hello World" Output-"HelloWorld".
Thanks, Vikram
Since you know you're only deleting characters and never inserting any, you can process a string in place with one traversal:
void remove_spaces(char* s) {
assert(s); // precondition: s is a non-null pointer
char* output = s;
for (; *s; ++s) { // *s is false when *s == '\0'
if (*s != ' ') { // if you want any whitespace, use !isspace(*s)
*output++ = *s;
}
}
*output = '\0';
}
Notice how output starts at s, so if there are no characters to remove, we write over each char with the same value. If there are spaces, however, s advances while output does not. Finally a null character is written to mark the end of the string.
int main() {
char input[] = "Hello, world!"; // make sure to use a writable buffer
remove_spaces(input);
puts(input);
return 0;
}
"Using ASCII or comparing" indicates some confusion. You should use ' '
instead of 32, even if you know they are equivalent on your system, for reasons of clarity.