The task is:

Text file contains words. Enclose words beginning with a consonant in double quotes.

Actually, as I try to solve it: We read text by character into an array of characters, if the character is capital, then we assign the current element to an empty variable, an opening quote to the current element, a variable to the next element, and a closing quote to the next element, and so with each element in the loop. The problem is that the compiler swears at the line "if (a [i] ==" A "," B ")" writes the following:

have incompatible types 'char' and 'char *'

Before that, I often wrote on php and there were no such problems.

#include <stdio.h> int main(int argc, char *argv[]) { char a[1000]; int count=0; FILE *file = fopen("1.txt", "rw"); for(int i=0;!feof(file);i++){ a[i]=fgetc(file); if(a[i] == 'A'){ } count++; } for(int i=0;i<count;i++){ printf("%c",a[i]); } fclose(file); printf("\n"); return 0; } 
  • one
    You are trying to compare a character ( a[i] ) with a pointer to characters ( "A" (yes, a string constant is an array of characters and, in a sense, means a pointer)), so the compiler swears at that. Well, and even if you write syntactically correctly if (a[i] == 'A','B') , then this will not be what you wanted. - avp
  • This is so clear how to fix it? - Vadim Moroz
  • Please note that you still incorrectly use feof . And the last thing - you shouldn’t fix the correct code in the question : you put everyone in a stupid position, including yourself: ask why the correct code doesn’t work ... - Harry
  • It is this code that is presented that gives exactly the error that was written - Vadim Moroz

1 answer 1

If this

 if(a[i] == "A","B") 

must mean a[i] or A , or B , then

 if (a[i] == 'A' || a[i] == 'B') 

or

 if (strchr("AB",a[i])) 

If not, explain what exactly you wanted to express with this construction ...

Here is an approximate solution to your problem:

 #include <stdlib.h> #include <stdio.h> #include <ctype.h> #include <string.h> int isConsonant(int c) { static char consonants[] = "BCDFGHKLMNPQRSTVWXZ"; return strchr(consonants,toupper(c)) != NULL; } int main() { FILE * in = fopen("1.txt","r"); if (in == NULL) { fprintf(stderr,"Can not open file\n"); return 1; } for(int word = 0, c = getc(in); c != EOF; c = getc(in)) { if (word) { if (isspace(c) || ispunct(c)) { if (word == 1) putchar('"'); word = 0; } } else if (!isspace(c) && (1 == (word = (isConsonant(c) ? 1 : 2)))) putchar('"'); putchar(c); } fclose(in); } 
  • character is uppercase or not. I know that it is incorrectly written there, if it will be easier for you, imagine that there is no "B" there, nothing will change because of this - Vadim Moroz
  • @VadimMoroz, and where did you find that the comparison should be done exactly as in your code? Your construction has no relation to the C language. - PinkTux
  • I wrote you how to proceed. Pay attention to single quotes. - Harry
  • I said that I know what I wrote wrong, the essence of the error was not in this, and I said this. The problem was exactly in the pointer, in that the element of the array could not be assigned a symbol, this is solved through a variable. - Vadim Moroz
  • Which variable? You did, as I wrote - through single quotes ... - Harry