0

i have to do an exercise where i need to acquire from keyboard a text, with 1000 max rows and every rows have a limit of 100 max characters.

Also i need to finish the acquisition of the input when the user type the row with the word "END" and i must show all the acquired rows without the "END" row, and also all the alphanumeric characters used, and at the end, the total number of used words.

I've tried to acquire the rows from keyboard through an array, but the for loop used for add rows, doesn't work well,indeed the loop ends after the first call.

I think this happens because the array is filled, but i don't know why and how to fix it.

This is my code:

#include <stdio.h>
#define MAX_ROWS 1000
#define MAX_CHARACTERS 100

int main() {
int i;
char text[MAX_CARACTERS];

for(i=0;i<MAX_ROWS && text!="END";i++) {

scanf("%98[^\n]",text);

}




return 0;   
}
Luca
  • 55
  • 7
  • Use `strcmp` to compare strings. See [this question](http://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c). – Etheryte Oct 31 '14 at 17:30
  • 'text!="END"' you cannot compare text like that. See strcmp(). – Martin James Oct 31 '14 at 17:31
  • 1
    Please [edit] your question title to something that describes the actual problem. We know you have a problem, or you wouldn't be posting here. We know it's to do with C, arrays, and strings, because you provided that information in the tags. That leaves no meaningful information in the subject. When editing, think about a title that will be useful to someone in the future searching here that sees the title in a search result. What would describe the problem to them so they know if it is useful with their problem? – Ken White Oct 31 '14 at 17:32
  • 1
    OP wrote *"This is my code:"*. No it isn't - `char text[MAX_CARACTERS];` has a typo, so this program won't have compiled. What else is different? How hard can it be to submit a complete program of about a dozen lines? – Weather Vane Oct 31 '14 at 18:27
  • I didn't catch this by my eye-compiler but that's true, complete program should be pasted. – soerium Oct 31 '14 at 18:50

3 Answers3

2

In C you have to use strcmp to compare strings:

for(i=0; (i < MAX_ROWS) && strcmp(text, "END"); i++)
Igor
  • 26,650
  • 27
  • 89
  • 114
1

You never read anything into text; but I'll assume that the testo here is meant to be text.

You don't compare C strings with == - that will just test whether two pointers point to the same address. You want strcmp().

char text[MAX_CARACTERS] = "";

for (i = 0; (i < MAX_ROWS) && (strcmp(text, "END") != 0); i++) {
  scanf("%98[^\n]",text);
}
Paul Roub
  • 36,322
  • 27
  • 84
  • 93
0

You may want to consider using strncmp as well

char text[MAX_CHARACTERS] = {0};

for (i = 0; i < MAX_ROWS && strncmp(text, "END", 3) != 0; i++) {
  scanf("%98[^\n]",text);
}

Or bytes sequence comparing, memcmp (not elegant one would say)

char text[MAX_CHARACTERS] = {0};

for (i = 0; i < MAX_ROWS && memcmp(text, "END", 3) != 0; i++) {
  scanf("%98[^\n]",text);
}
soerium
  • 573
  • 4
  • 12