almrog
almrog

Reputation: 143

Program to get an indefinite number of strings in C and print them out

As part of an assignment, I am supposed to write a small program that accepts an indefinite number of strings, and then print them out. This program compiles (with the following warning

desafio1.c:24:16: warning: format not a string literal and no format arguments [-Wform
at-security]                                                                          
         printf(words[i]);

and it prints the following characters on the screen: �����8 ���@Rl�. I guess it did not end the strings I entered by using getchar properly with the null byte, and it prints out garbage. The logic of the program is to initiate a while loop, which runs untill I press the enter key \n, and if there are an space, this is a word that will be store in the array of characters words. Why am I running into problems, if in the else statement once a space is found, I close the word[i] = \0, in that way and store the result in the array words?

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

int main()                                                                        
{
    char words[100][100];
    int i,c;
    char word[1000];

    while((c = getchar()) != '\n')                                                
    {                                                                             
         if (c != ' '){                                                            
            word[i++] = c;                                                         
            c = getchar();                                                         
          }                                                                        
         else{                                                                     
             word[i] = '\0';                                                       
              words[i] == word;                                                     
          }                                                                         
      }                                                                             
      int num = sizeof(words) / sizeof(words[0]);                                   
      for (i = 0; i < num; i++){                                                    
          printf(words[i]);                                                         
      }                                                                             
     return 0;                   
} 

Upvotes: 0

Views: 245

Answers (1)

Chris Loonam
Chris Loonam

Reputation: 5745

Here are some fixes to your code. As a pointer (as mentioned in other comments), make sure to enable compiler warnings, which will help you find 90% of the issues you had. (gcc -Wall)

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

int main() {
    char words[100][100];
    int i = 0;
    int j = 0;
    int c;
    char word[1000];

    while((c = getchar()) != '\n') {
        if (c != ' '){
            word[i++] = c;
        } else {
            word[i] = '\0';
            strcpy(words[j++], word);
            i = 0;
        }
    }
    word[i] = '\0';
    strcpy(words[j++], word);

    for (i = 0; i < j; i++) {
        printf("%s\n", words[i]);
    }

    return 0;
}
  1. i was uninitialized, so its value was undefined. It should start at 0. It also needs to be reset to 0 after each word so it starts at the beginning.
  2. The second c = getchar() was unnecessary, as this is done in every iteration of the loop. This was causing your code to skip every other letter.
  3. You need two counters, one for the place in the word, and one for the number of words read in. That's what j is.
  4. == is for comparison, not assignment. Either way, strcpy() was needed here since you are filling out an array.
  5. Rather than looping through all 100 elements of the array, just loop through the words that have actually been filled (up to j).
  6. The last word input was ignored by your code, since it ends with a \n, not a . That's what the lines after the while are for.
  7. When using printf(), the arguments should always be a format string ("%s"), followed by the arguments.

Of course, there are other things as well that I didn't fix (such as the disagreement between the 1000-character word and the 100-character words). If I were you, I'd think about what to do if the user entered, for some reason, more than 1000 characters in a word, or more than 100 words. Your logic will need to be modified in these cases to prevent illegal memory accesses (outside the bounds of the arrays).

As a reminder, this program does not accept an indefinite number of words, but only up to 100. You may need to rethink your solution as a result.

Upvotes: 2

Related Questions