Search code examples
cspacetrim

I've a text file. The text file contains strings. I wanna remove space from end of the sentence by using C programming


There is some question regarding my question but I can not find the exact answer from those related questions. That's why I'm asking. Please try to understand and provide me a Function by which I will be able to get the correct answer.

I've a text file. Say it's "filename.txt" The file contain some strings. And at the end of every sentence there is a extra space.

For Example: The input file:

high blood sugar levels (space)
type 2 diabetes definition (space)
high blood sugar symptoms 
glucose tolerance test 
symptoms of high blood sugar 

Here after every sentence there is an extra space. I would like to remove those spaces.

I want to generate a output file like this:

high blood sugar levels,
type 2 diabetes definition,
high blood sugar symptoms,
glucose tolerance test,
symptoms of high blood sugar

I've tried this, but the space is still before comma(,).

#include <stdio.h>
#include <stdlib.h>

int main()
{
    fopen("out.txt", "w", "stdout");
    char ch, keywords[25];
    FILE *fp;

    fp = fopen("keywords.txt","r"); // read mode

    if( fp == NULL )
    {
        perror("Error while opening the file.\n");
        exit(EXIT_FAILURE);
    }

    printf("The contents of %s file are :\n", "keywords.txt");

    while( ( ch = fgetc(fp) ) != EOF )
    {
        if(ch=='\n') ch=',';
        printf("%c",ch);
    }

    fclose(fp);
    return 0;
}

Please also guide me, How to print my result to a output file. I will use this simple program for my personal task. That's why I'm trying to find the result. Please help me.


Solution

  • You are not taking any action on trailing white spaces (the ones that occur after a sentence is over and before the newline char '\n'). All you are doing is incorrectly trying (because you are only using printf which does not write to the file, so no change to your txt file anyways) trying to replace '\n' by a comma, which if done correctly would result in the following output containing only one line with all spaces intact:

    high blood sugar levels (space),type 2 diabetes definition (space),high blood sugar symptoms,glucose tolerance test,symptoms of high blood sugar,
    

    In order to get rid of the trailing spaces, you got to count number of spaces every time you see them and reset the count if you get a word instead of a newline. Otherwise, if you do get a newline while counting whitespace, use that count value to print that many backspaces '\b' followed by printing a comma. Note that you have not removed any whitespaces other than where you added comma, so newline for each line is still at the same place after trailing whitespaces. The tricky part is the corner case of no trailing space, that is '\n' immediately after the last char of a sentence. Inserting a comma here between the last char and newline, would requiring moving data in the file to make space for this comma. Thus, the best way would be to use a temp file, build it line by line from input file after removing trailing spaces and adding a comma. You might want to use fgets and fputs