2

If i want to take an input in a 2d array with each string in one row, and the next in the other(i.e. change the row on pressing enter). How can i do that in C. C doesnt seem to have convenient "String" Handling. I obviously mean doing so without the use of getchar().

1
  • 2
    The fgets function is just fine, combined with some malloc. Commented Jan 26, 2013 at 10:42

4 Answers 4

5

3 ways are there which are mentioned below.

If you know the maximum number of strings and maximum number of chars, then you can use the below way to declare a 2D character array.

char strs[MAX_NO_OF_STRS][MAX_NO_CHARS] = {0};
for (i = 0; i < MAX_NO_OF_STRS; i++)
{
    scanf("%s", strs[i]);
}

If you know the maximum number of strings, and you dont want to waste the memory by allocating memory for MAX_NO_CHARS for all strings. then go for array of char pointers.

char temp[MAX_NO_CHARS] = {0};
char *strs[MAX_NO_OF_STRS] = NULL;
for (i = 0; i < MAX_NO_OF_STRS; i++)
{
    scanf("%s", temp);
    strs[i] = strdup(temp);
}

If you know the maximum number of strings during run time means, you can declare a double pointer of char. Get the number of strings n from user and then allocate memory dynamically.

char temp[MAX_NO_CHARS] = {0};
char **strs = NULL;
int n = 0;
scanf("%d", &n);
strs = malloc(sizeof(char*) * n);
for (i = 0; i < n; i++)
{
    scanf("%s", temp);
    strs[i] = strdup(temp);
}
Sign up to request clarification or add additional context in comments.

3 Comments

In the first example that you have given, will there be a '\0' or '\n' stored in the array, as a sentinel to mark the end of the string?
@PranavSharma, there will be. But please note that using %s without a size limit (%255s for instance) puts you at risk of a buffer overflow.
The first example, doesnt seem to work properly: - it puts every word in a separate row once it encounters a space, whereas the requirement is to enter a new row everytime an enter is encountered, to take the next string input.
4
#include<stdio.h>

main()

{

char student_name[5][25];

    int i;

    for(i=0;i<5;i++)
    {
       printf("\nEnter a string %d: ",i+1);
       scanf(" %[^\n]",student_name[i]);
    }

}

u can read strings using 2d array without using getchar() by putting space in scanf(" %[^\n]") ; before %[^\n]!

5 Comments

However on pressing enter, the input is stopping. The requirement is to stop the input after 5 names have been fed. The input stops after feeding just one name and pressing enter.
Oh okay! Jeez, i had omitted that thinking it was some kind of a typo. My bad. Thank you so much StoryTeller.
ya it was my answer! btw no need 2 thnk me! i'm jst here to help!!! if u another question about C u r free to ask me!
@StoryTeller it doesn't make difference if u write code without putting space!
@BrijeshGajjar, what are you talking about?
0

An alternative to using malloc and filling up an array of pointers with buffers of a fixed size, would be to allocate a 2d array (in static storage or on the stack) and fill it up. KingsIndian modifed code example would than look like this:

#include <stdio.h>

int main()
{
 char str[2][256] = {{0}};
 int i = 0;

  for(i=0;i<2;i++)
  {
    scanf("%255s", &str[i][0]);
  }
  return 0;
}

If all strings you expect to get are no longer than some size, than this approach will spare you the need to deal with freeing the memory yourself. It is less flexible however, meaning that you can't fit the size of an individual buffer to the string it contains.

EDIT

Adding to the information in the comment, to read a string that is terminated only by a new-line, rather than by any whitespace:

scanf("%255[^\n]", str[i]);

1 Comment

Lets say i want to print the stored data, character by character. When i do that, it seems that my program encounters a null character '\0' after every space in my string, and hence prints it in a new line(in my terminal window). This implies that everytime a space is encountered, my program has essentially stored it in a new row. Which is not the requirement.
0

I prefer no to use scanf. Instead you can use fgets.

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

int main()
{
 char str[array_size][char_size] = {{0}};
 int i = 0;

  for(i=0;i<array_size;i++)
  {
    fgets(str[i], array_size, stdin);
    getchar(); // get char is used to clear the buffer and get rid of the new line fgets leaves behind
  }
  return 0;
}

1 Comment

yeah sorry i didn't see that, thanks for teling me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.