Md Soman Khan
Md Soman Khan

Reputation: 25

How to take string input in different lines?

I wanna take some strings as input in 5 different lines.There is no limit about how many words a line can contain.But I want 5 lines.How can I do it?Please help me.

Example:The program will be like the following one

How many Line?

5

Enter 5 line:

hi i am john

i am new to c programming.

in have problem in string.

i can not take input

pleas help me.thanks a lot.

Here is my code:

 #include<stdio.h>
 int main()
 {
    int line,i;
    char *string;
    printf("How many line?\n");
    scanf("%d",&line);
    for(i=0;i<line;i++)
    {
        gets(string);
    }
    printf("You entered:\n");
    for(i=0;i<line;i++)
    {
      puts(string);
    }
    return 0;
 }

In my code it is just taking (n-1) line.Like if i give line 4 it takes 3 line and print the only the last line for 4 times.How can I solve this?

Upvotes: 0

Views: 3682

Answers (2)

David C. Rankin
David C. Rankin

Reputation: 84521

Rather than statically specifying a 2D array, you are better served by specifying an array of pointers and then allocating memory for each string added to the array. While fgets or getline is preferred, there is nothing wrong with allocating memory with scanf. (Note: the current version of scanf uses m prior to the conversion specifier for allocation, windows and earlier version used a for this purpose). When using scanf to allocate, scanf expects a pointer argument of type char ** to hold the allocated string. When you allocate memory, regardless of how, you are responsible for freeing the memory when it is no longer required. Taking that into consideration, your code could be written as follows:

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

#define MAXD 64

int main() {

    char *string[MAXD];
    int line = 0;
    int i = 0;

    printf ("\nHome many lines: ");
    scanf ("%d%*c", &line);

    if (line > MAXD) {  /* validate line <= MAXD pointers */
        fprintf (stderr, "warning: request exceeds %d limit.\n", MAXD);
        line = MAXD;
    }

    /* scanf will allocate if given 'm' ('a' on Win & earlier versions) 
    as part of the specification before the conversion specifier */
    for (i = 0; i < line; i++) {
        printf ("\n input: ");
        scanf ("%m[^\n]%*c", &string[i]);
    }

    printf ("\nYou entered:\n\n");
    for (i = 0; i < line; i++)
        printf ("  string[%d] : %s\n", i, string[i]);

    printf ("\n");

    /* free the memory allocated by scanf */
    for (i = 0; i < line; i++)
        free (string[i]);

    return 0;
}

Use/Output

$ ./bin/scanf_string_dyn

Home many lines: 4

 input: this is line 1

 input: this is line 2

 input: this is line 3

 input: this is line 4

You entered:

  string[0] : this is line 1
  string[1] : this is line 2
  string[2] : this is line 3
  string[3] : this is line 4

You can adjust the number of pointers by adjusting the MAXD define. (you can also dynamically allocate and reallocate them as necessary, but that is beyond the scope of your question)

Upvotes: 1

Santosh A
Santosh A

Reputation: 5351

The bug in your code is that you have allocated memory for the character string to store the user input.

Here in the code char *string is a pointer, which points to the lastly entered string from the user input. Hence you get the last string as the output instead of fetching all the strings.

Change your code as below:

#include<stdio.h>
int main()
{   
    int line,i;
    char string[50][80];
    printf("How many line?\n");
    scanf("%d",&line);

    for(i=0;i<line;i++)
    {   
        scanf("%s", string[i]);
    }
    printf("You entered:\n");
    for(i=0;i<line;i++)
    {   
        puts(string[i]);
    }
    return 0;
}

In this code the maximum number of lines (rows) that can be read to set to 50 and the maximum width of each line is set to 80.

If you are not sure of the maximum number of lines to be read or width of each line, then you should allocate memory for each line read dynamically using malloc and free them at the end of the program before return using free.

Also avoid using gets() for these reasons

Upvotes: 3

Related Questions