OrangeBike
OrangeBike

Reputation: 145

Reading words and storing them in 2-D array in C

I am trying to write a program that reads lines word by word and puts each word in an array (later I want to do some operations on those words but this isn't an issue now) and in the output there should be a number of words in each line. For example:

input:
good morning my neighbors!
how are you?

output:
4
3

Here is my code:

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

int main()
{
    char word[100];
    char wordArray[100][100];
    int count = 0;
    for(;scanf("%s", word)!=EOF;)
    {
        strcpy(wordArray[count], word);
        count++;
    }
    printf("%d", count);

    return 0;
}

But it gives me just 7 in the output (number of all words on both lines). If I put printf function inside the for loop then I get 1234567 as an output. How do I make it count words on one line and print it then set the count to zero and start over on the next line?

Upvotes: 0

Views: 288

Answers (3)

anastaciu
anastaciu

Reputation: 23792

It gives you 7 because count is never reseted. I would store the counts to print later, this could be done with an array, the 1234567 is normal if it's inside the cycle it will print count for every word scaned. If you use "%d " with a space you'll have 1 2 3 4 5 6 7.

The following program takes 5 lines from the user, counts the words in each line and stores the counts in an array, using scanf and strtok to count the words:

Demo

#include <stdio.h>
#include <string.h>
#define SIZE 5
#define LENGTH 100

int main()
{
    char word[LENGTH];
    char wordArray[SIZE][LENGTH];
    int count[SIZE] = {0}; //array to store the counts
    const char delimiter[] = " ";
    char *token;
    printf("Insert %d phrases:\n", SIZE);
    // %99[^\n] specifier also to avoid buffer overflow and read till newline
    for (int i = 0; i < SIZE && scanf(" %99[^\n]", wordArray[i]) == 1; i++)
    {
        strcpy(word, wordArray[i]);
        token = strtok(word, delimiter);
        while (token != NULL)
        {
            count[i]++;
            token = strtok(NULL, delimiter);
        }
    }
    for (int i = 0; i < SIZE; i++) {//print all the counts
        printf("string %d - \"%s\" - ", i + 1, wordArray[i]);
        printf("%d words\n", count[i]);
    }
    return 0;
}

Upvotes: 1

NAND
NAND

Reputation: 685

Here I used fgets to get line by line from stdin. The code actually count the spaces in the line and add 1 to them. I also did NOT used string.h.

#include <stdio.h>
#define SIZE 100

int main()
{
  int i=0;
  char lines[SIZE][SIZE];
  int count[SIZE]={0};
  while(i<SIZE){
    fgets(lines[i], SIZE, stdin);
    if (lines[i][0]=='\n') break;
    i++;
  }

  for(int j=0; j<i; j++){
    char *x=lines[j];
    while(*x!='\0') {
      if (*x==' ') count[j]++;
      x++;
    }
    printf("%d\n", count[j]+1);
  }
  return 0;
}

Upvotes: 1

Hitokiri
Hitokiri

Reputation: 3689

You should use fgets or getline because scanf reads word by word not all words in one line.

The code:

#include <stdio.h>
#include <string.h>
#define SIZE 100

int main()
{
    char wordArray[SIZE][SIZE];
    int count[SIZE];
    int c = 0;
    while(fgets( wordArray[c], SIZE, stdin) && c < SIZE)
    { 
        for(int i = 0; wordArray[c][i] != '\0' ; i++) {
            // remove enter character at the end of each line, not necessary in this case but mabe later when you work with all works
            wordArray[c][strcspn ( wordArray[c], "\n" )] = '\0'; 
            if (wordArray[c][i] == ' ') {
                 count[c]++; // increase the number of words if we meet space character
            }
        }
        c++;
    }
    for (int i = 0; i < c; i++) //print all the counts
        printf("%d", count[i] + 1);

    return 0;
}

Upvotes: 1

Related Questions