Evan Bloemer
Evan Bloemer

Reputation: 1081

C - Filling a 2D array using getchar

I'm trying to fill a 2D array with \n being the separator. This is the user input:

S T E L B M T F E Y D E E P S R T C I A E E
N N E L I T R L B D E T A R E M U N E T Y L
N O I T A N I M I R C N I F L E S S E N T A
A U I D E W A R R A N T N U P R U S S E R P
P G S G E A L P A P B A N P S A S S N M E A
C O N S I T U T I O N D E E C W S O O H P D
S V W D E L A N E E J A M E S M A D I S O N
A E D E S N E G R J C U L T N O H L T I R A
A R C E R R T R E E S B O N E E I D N N P R
S N J U D I C I A L A S S E C O R P E U D I
S M R A R A E B W B E S S M E O A U V P E M
O E O I A I L N O U C D O D S S E N N I G R
L N I D G Y T R C O M P E N S A T I O N N D
D T O Z E H P Y N D R L E E A O H S C O I B
I T P S U E T G O L U Z M M R B E H P I R T
E O I E A R R S U U I B H A Y L L M S T F A
R I N R E E E F U T L V Q U A R T E R I N G
S I D B S R R D I Y E N I G M I A N A T I R
S Q I S E B S C N S P E E C H R O T A E Y N
D L C M I L I T I A F L R N C A T S S P S E
R U T E D Y L E B I L C O H M L E T E S Y Y
L S T R T E W Z L I O S A E N S A E I Y A L

This is my code:

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

  int findSize();

  char grid[50][50];
  int row = 0, col = 0, size;
  int x, y;
  char c;

  int main()
  {
      size = findSize();

      for(x = 0; x < size; x++)
      {
          while ((c = getchar()) != '\n')
          {
              if (!isspace(c))
              {
                  grid[row][col++] = c;
              }
          }
          row++;
      }

      for(x = 0; x < size; x++)
      {
          printf("%s\n", grid[x]);
      }
  }

  int findSize()
  {
      int counter = 0;
      while ((c = getchar()) != '\n')
      {
          if (!isspace(c))
          {
              grid[row][col++] = c;
              counter++;
          }
      }
      return counter;
  }

This is my output:

STELBMTFEYDEEPSRTCIAEENNELITRLBDETAREMUNETYL

IMIRCNIFLESSENTA


NPSASSNMEA


ISON



JUDICIALASSECORPEUDI


OUCDODSSENNIGR


AOHSCOIB


FA

What am I doing wrong? Am I printing it wrong or putting it to the 2D array wrong? I am well versed in java, but not so much C.

Upvotes: 0

Views: 1294

Answers (2)

Jcao02
Jcao02

Reputation: 804

Maybe using getline is simpler. Here's an example

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


int main() {
    
    char *line = NULL;
    size_t size; 
    char grid[50][50]; 


    int sizeG = 0; 
    while (getline(&line, &size, stdin) != -1) {
        int s = strlen(line);
        line[s - 1] = '\0'; 
        strcpy(grid[sizeG++], line); 
    }
    
    for (int i = 0; i < sizeG; ++i) {
        printf("%s\n", grid[i]); 
    }
}

You use stdin in the stream parameter so the program reads the standard input. The line[s - 1] = '\0' is because getline includes the \n character but not the end of string character.

Update

This solution is assuming the max number of characters in a row is 50 and the max number of rows if 50 too.

Upvotes: 1

Adam Sosnowski
Adam Sosnowski

Reputation: 1284

There is row increment and column reset missing in findSize(). Column reset is missing after row increment in main.

Upvotes: 1

Related Questions