Home > Net >  How to delete characters except alphabets in c
How to delete characters except alphabets in c

Time:09-20

There is a file "poem.txt":

*The ho$use cat sits.* 

*And sm%iles and) sing&s.*

*He% know*(s a l_ot* 

*Of s!ecret thi<ngs.*

I need to delete unnecessary symbols from it and write it to another file "poem_modified" without using arrays, functions, structures and pointer and only with <stdio.h> library:

I was able to do it so far:

#include <stdio.h>

int main() {
FILE *input;
FILE *output;

input  = fopen ("poem.txt", "r");
output = fopen ("poem_modified.txt", "w");

if (input == NULL || output == NULL)
{
    printf("Problem! \n");
    return 1;
}
char ch ;
while((ch=getc(input)) != EOF)

fprintf(output, "%c", ch);

fclose(input);
fclose(output); 
}

CodePudding user response:

Adding conditions while printing the character can help Suppose, it is required to include a-z and A-X only with spaces and newline char. So conditions can be made such as if the character is between a-z or between A-Z or it is newline or space, the char will be printed. Otherwise not. Any other conditions can be added.

The getc() function return type is an integer. documentation

Correct indentation helps to understand the code.

#include <stdio.h>

int main() {
    FILE *input;
    FILE *output;

    input  = fopen ("poem.txt", "r");
    output = fopen ("poem_modified.txt", "w");

    if (input == NULL || output == NULL)
    {
        printf("Problem! \n");
        return 1;
    }
    int ch ;

    while((ch=getc(input)) != EOF) {
        if ((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || ch == ' ' || ch == '\n'){
            fprintf(output, "%c", ch);
        }
    }

    fclose(input);
    fclose(output);
}

output :

The house cat sits

And smiles and sings

He knows a lot

Of secret things

CodePudding user response:

You can use the various functions from ctype.h to check if something belongs to a certain category of symbols. For example isalpha checks if a character is a letter and isspace checks if it's a space or new line character etc. By using these two functions in combination, we can chose to only print characters that are either letters or spaces. Example:

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main (void)
{
  char input[] = "*The ho$use cat sits.*\n"
                 "*And sm%iles and) sing&s.*\n"
                 "*He% know*(s a l_ot*\n"
                 "*Of s!ecret thi<ngs.*\n";

  size_t length = strlen(input);
  for(size_t i=0; i<length; i  )
  {
    if(isalpha(input[i]) || isspace(input[i]))
    {
      putchar(input[i]);
    }
  }
}

Apart from the ctype.h functions making the code easier to read, manual checks like ch >= 'A' && ch <= 'Z' are strictly speaking not well-defined or portable. Because C doesn't guarantee that letters are placed adjacently in the symbol table (see for example the EBCDIC, which was a format used in the Jurassic era). Also the ctype.h functions might handle "locale-specific" characters outside the classic 7 bit ASCII.

  •  Tags:  
  • c
  • Related