0

Here is my code:

#include<stdio.h>

int main()
{
    int a = '\0';

    while ((a = getchar()) != EOF){
        if (a != ' ' || a != '\t' || a != '\n' || a != ';' || a != ',' || a != '.'){
            putchar(a);
        }
        else{
            continue;
        }
    }

    system("pause");
    return 0;
}

I need to read a poem from an input file, and output the poem with no spaces or punctuation. Must be done using I/O variation. I've searched all over, but I can't seem to find how to do this the way I need. Any help is much appreciated...

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
Tom
  • 1
  • Try checking this link for information: http://stackoverflow.com/questions/19138983/c-remove-punctuation-from-string – Veer Singh Sep 30 '15 at 00:52
  • 1
    What do you mean by, *I/O variation*? If you mean *I/O redirection*, then just use the redirection operators on the command line: `mycommand < input_file > output_file`. That takes standard input from `input_file` and sends standard output to `output_file`. But you'll probably need to get rid of that `system("pause")` call. – lurker Sep 30 '15 at 01:33
  • 1
    Your `if` makes no sense. Just like every animal is either not a dog or not a cat, every character is either not a space or not a tab. So your `if` catches everything. If you want to find only animals that are neither dogs nor cats, you need to look for animals that are not dogs *AND* are not cats. – David Schwartz Oct 01 '15 at 23:07

1 Answers1

-1

Hope this solves your Problem. Use ascii values for character. Also use fgetc/fputc/fscanf/fprintf etc for file i/o related operations. ASCII CHART link here ASCII Chart VALUES.

#include<stdio.h>


int main()
{
 FILE *pfile=NULL;
 char data[255];
 int i=0;

 pfile=fopen("poem.txt","r");

 while((data[i]=fgetc(pfile)) != EOF)
 {

   if(data[i]> 32 && data[i] < 123)
   {
     if(data[i] != 44 && data[i]!= 45 && data[i]!=46 && data[i]!=33)
     {
       //In the above 'if statement' you can add more characters/special
       //characters you wish to exclude/ignore
       printf("%c",data[i]);
       i++;
     }
   }

}
 return 0;
}
Mutex202
  • 43
  • 2
  • I would highly suggest not using the ASCII numbers but instead the characters. Using the numbers makes determining the intent of the program difficult. Of course there are options such as `isalpha` and `isspace` which are better than either approach when appropriate. – missimer Oct 02 '15 at 22:08
  • I hope you realize that 'char' in C is really intended to be the smallest 'integer' rather than to be 'character' data type. And if you compare a character data type to a number it will always by default compare the ASCII value of the character with the number. You need to explicitly typecast it to get the character instead of ascii value. SO why extra overhead when you can directly compare them with numeric value?! lol. – Mutex202 Oct 02 '15 at 22:57
  • Actually in C, a character constant is of type `int` just as the decimal constants you used. Also your code and the same code replacing the numerical constants with character constants compiles into the same x86 assembly, (with and without -O2 on gcc). See http://stackoverflow.com/questions/11310456/is-the-integer-constants-default-type-signed-or-unsigned and http://stackoverflow.com/questions/20764538/type-of-character-constant. The only extra conversion is occurring at compile time not run time. – missimer Oct 02 '15 at 23:25