I'm trying to read from a text file and have any multiple whitespaces between characters become one whitespace. If this how I do it using an array,
#include <stdio.h>
//Function prototype
void modifyspace(char *dest, char *src);
int main()
{
/* Initialize an array of 2000 characters */
char src[2000] = "there are many space in this line";
/* Initialize dest array which */
char dest[strlen(src) + 1];
/* Calls the modifyspace function. */
modifyspace(dest, src);
/* Displays a blank space between the output and everything else. */
printf(" \n");
/* Display characters currently in the src array of characters */
printf("%s\n", src);
/* Displays the same src array of characters but each word only has one space
between them */
printf("%s\n", dest);
return 0;
}
how can I do it reading from the file?
#include <stdio.h>
int main()
{
FILE *fp;
int i;
fp = fopen("singlespace.txt", "r");
if(fp == NULL) printf("No such file exists \n");
else{
do {
i = getc(fp);
putchar(i);
}while (i != EOF);
}
fclose(fp);
/* Displays a blank space between the output and everything else. */
printf(" \n");
return 0;
}