Home > Software engineering >  Input for 2D character array in C
Input for 2D character array in C

Time:10-17

I am new to C programming....we have 2D arrays for integers...but how to declare and take the input for 2D string arrays...I tried it for taking single character input at a time similar to integer array...but I want to take whole string as input at a time into 2D array..

code:

#include <stdio.h>

int main()
{
    char str[20][20];
    int i, j;

    for (i = 0; i < 20; i  )
    {
        for (j = 0; j < 20; j  )
        {
            scanf("%c", &str[i][j]);
        }
    }
}

can anyone resolve this problem?

CodePudding user response:

A few issues with your code. Using scanf to reach characters, you're going to read newlines. If I create a more minimal version of your code with an extra few lines to print the input, we can see this:

#include <stdio.h>

int main() {
    char str[3][3];
    int i, j;

    for (i = 0; i < 3; i  ) {
        for (j = 0; j < 3; j  ) {
            scanf("%c", &str[i][j]);
        }
    }

    for (i = 0; i < 3; i  ) {
        for (j = 0; j < 3; j  ) {
            printf("%c ", str[i][j]);
        }
        printf("\n");
    }
}

And running it:

$ ./a.out
gud
ghu
ert
g u d

 g h
u
 e
$

We can test the input to circumvent this. If the character input is a newline character ('\n') then we'll decrement j so effectively we've sent the loop back a step. We could easily extend this boolean condition to ignore other whitespace characters like ' ' or '\t'.

#include <stdio.h>

int main() {
    char str[3][3];
    int i, j;

    for (i = 0; i < 3; i  ) {
        for (j = 0; j < 3; j  ) {
            char temp = 0;
            scanf("%c", &temp);

            if (temp == '\n' || temp == ' ' || temp == '\t') {
                j--;
            }
            else {
                str[i][j] = temp;
            }
        }
    }

    for (i = 0; i < 3; i  ) {
        for (j = 0; j < 3; j  ) {
            printf("%c ", str[i][j]);
        }
        printf("\n");
    }
}

Now,, when we run this:

$ ./a.out
gud
ghu
ert
g u d
g h u
e r t
$

Of course, one other thing you should do is to check the return value of scanf, which is an int representing the number of items read. In this case, if it returned 0, we'd know it hadn't read anything. In that case, within the inner loop, we'd probably also want to decrement j so the loop continues.

CodePudding user response:

#include<stdio.h> 
main()
{
char name[5][25];
int i;
//Input String
for(i=0;i<5;i  )
{
   printf("Enter a string %d: ",i 1);
}
//Displaying strings
printf("String in Array:\n");
for(int i=0;i<5;i  )
  puts(name[i]);
}

CodePudding user response:

The declaration of a 2D string array can be described as: char string[m][n]; where m is the row size and n is the column size. If you want to take m strings as input with one whole string at a time...it is as follows

#include<stdio.h>
int main()
{
char str[20][20];
int i,j;
for(i=0;i<m;i  )
{
      scanf("%s",str[i]);
}
}

here 'i' is the index of the string.... Hope this answer helps...

  • Related