Possible Duplicate:
Consecutive Blank space Removal in C
Prompt:
A text file contains a bunch of characters. There are no tab characters within the file. Write a program that replaces two or more consecutive blanks by a single blank. The input from this program should come from a file whose name has been supplied via argv[1]. The output from this program should go to standard output.
Question: How to remove consecutive blank spaces in a text file?
I have started my code and it compiles but then doesn’t do anything. Im not sure where I went wrong with the code. I want the code to basically simulated a FMS which says that if the text read in is equal to a blank space two times in a row then place the text from the file there but I am having trouble getting it to print out correctly.
Input:
Let’s go to the movies.
Output:
Let’s go to the movies.
My written Code:
#include <stdio.h>
int main(int argc, char* argv[]){
int i;
char c;
FILE* fin;
fin=fopen("textfile38", "r");
fscanf(fin,"%c", &c);
while((i=getchar()) !=EOF)
putchar(c);
if(i ==' ')
{
putchar(i);
}
else
{
putchar(' ');
}
printf("%c \n", c);
return 0;
}
I get it to return L. Am I not reading in all of the characters? Any help would be appreciated thanks.
UPDATED CODE:
#include <stdio.h>
int main(int argc, char* argv[]){
char c;
FILE* fin;
fin=fopen("textfile38", "r");
while(fscanf(fin,"%c", &c) !=EOF){
if(c ==' ')
{
putchar(c);
}
else
{
putchar(' ');
}
printf("%c", c);
}
return 0;
}
Only problem is that the spaces are still there from the input, also its printing vertically not horizontally, and i don’t know why.
You probably need to keep state as to what you have seen, so something like….