I'm trying to write exercise of the "C Primer Plus" book. In one of them, I faced something that I couldn't solve or figure out what's going on. After step-by-step debugging trials, I just tested this:
#include <stdio.h>
int main()
{
char str[500];
gets(str);
puts(str);
return 0;
}
the output (as desired) :
exact ly every thing I enter
exact ly every thing I enter
But about the exercise I'm trying to do, it's sensitive to more than 2 successive spaces. the gets()
is just followed by the puts()
; but I don't know what's wrong. So I quote the whole code:
/*
BUG: MORE THAN 1 SPACES NEXT TO EACHOTHER. WHERE THE FIRST CHARACTER GOES?!!
Write a function that takes a string as an argument and removes the spaces from the string.
Test it in a program that uses a loop to read lines until you enter an empty line.
The program should apply the function to each input string and display the result.
*/
#include <stdio.h>
#include <string.h>
int spaceRemover(char *in);
void takeBack(char *in);
int main(void)
{
puts("Enter a string for the SPACE-REMOVER: (RETURN to quit)");
do
{
char str[500];
int spaces;
gets(str);
puts(str); //for debugging to know is it complete just after gets() ?
//printf("\nFirst char of string: %c\n",str[0]);
//printf("\nFirst Char: %p '%c'\n",str,*str);
spaces=spaceRemover(str);
printf("\n%d spaces removed: \n",spaces);
puts(str);
puts("\nEnter a string for the SPACE-REMOVER: (RETURN to quit)");
}
while (getchar() != '\n');
return 0;
}
int spaceRemover(char *in)
{
int count=0, i;
for (i=0 ; i<strlen(in) ; i++)
while ( *(in+i)==' ' ) //IF will skip more than one space; but WHILE won't
{
//printf("%p '%c' \t B4: %p '%c'\n",in+i,*(in+i),in+i-1,*(in+i-1));
takeBack(in+i);
count++;
}
return count;
}
void takeBack(char *spaceLocation)
{
int j=0;
while (*(spaceLocation+j)!= '\0' )
{
*(spaceLocation+j) = *(spaceLocation+j+1);
//putchar(*(spaceLocation+j+1));
j++;
}
return;
}
the output:
Enter a string for the SPACE-REMOVER: (RETURN to quit)
this is separated by single spaces
this is separated by single spaces
5 spaces removed:
thisisseparatedbysinglespaces
Enter a string for the SPACE-REMOVER: (RETURN to quit)
I'll try more than single space separators
'll try more than single space separators
13 spaces removed:
'lltrymorethansinglespaceseparators
NOTE: using the Blockquote with this one, discards the successive spaces.
what's going on here? is there anything wrong with my code, causing this?
(using Code::Blocks with gcc.)