-1

hi Iam new to programming i have a problem in reading a multiple strings can anyone help me.

    #include<stdio.h>
    #include<string.h>
    int main()
    {
       int a;
       char n[50];
       scanf("%d",&a);
       for(int i=0;i<a;i++)
       {
          scanf("%s",n[i]);
       }

       for(int i=0;i<a;i++)
       {        
          printf("%s\n",n);
       }

       return 0;

    }
Tormund Giantsbane
  • 355
  • 1
  • 4
  • 12
SAI KUMAR
  • 1
  • 1
  • 1
  • 2
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Mar 28 '18 at 19:09
  • [How do I create an array of strings in C?](//stackoverflow.com/q/1088622) – 001 Mar 28 '18 at 19:16
  • 1
    I think the compiler already helped you by emitting a warning. Didn't it? – Arndt Jonasson Mar 28 '18 at 19:16
  • 1
    What makes you think that you have a problem? Compiler error? Quote it. Misbehaviour? Describe it. Crash? Debug it. – Yunnosch Mar 28 '18 at 19:20
  • There are some very relevant comments here so far, and you should heed them, but few address your immediate issue. (which could have been satisfied by a little better search before asking here) There are several viable answers addressing your question _[HERE](https://stackoverflow.com/questions/314401/how-to-read-a-line-from-the-console-in-c)_. – ryyker Mar 28 '18 at 19:30
  • @Yunnosch So basically there's never a time when asking for help is the right choice? :) – unwind Mar 28 '18 at 19:50
  • n[i] is not a string, so reading with %s into n[i] is wrong. – FredK Mar 28 '18 at 20:03
  • @unwind It is not about the time. It is about how unnecessarily hard you make helping you. Yell "Help!" or explain what information you have on your problem. What do you think might be the quicker way? – Yunnosch Mar 28 '18 at 20:54
  • You cannot use `scanf ("%s"...)` without providing the *field-width* modifier to protect your array bounds. See [man 3 scanf](https://man7.org/linux/man-pages/man3/sscanf.3.html). Failure to provide the field-width modifier makes your code exploitable by buffer-overrun. The use of `scanf ("%s", ...)` is no safer than using `gets()`, see: [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/q/1694036/3422102) Likewise you cannot use any input function correctly unless you ***check the return***, especially when numeric conversions are involved. – David C. Rankin Oct 22 '22 at 06:31

3 Answers3

0

The value of 'a' must be (total length of string)+1 and always less than max size of array. In first for loop:

     scanf("%s",&n[i]);

If you use scanf then while entering input you need to press [Enter] after each letter. Using getchar() instead of that scanf will let you type the entire string at once. Use this instead of scanf:

    n[i]=getchar();

Lastly, that second for loop is not needed,

    printf("%s\n",n); 

alone will print the entire string.

VH97
  • 13
  • 4
0

You have to define 2-D array not just 1-D

char n[number_of_strings][each_string_length]

Be careful of your buffer overflow too. Using scanf() is not safe, you should limit your string length by not exceeding.

each_string_length-1

Strings in C have a '\0' null terminator to let compiler recognize it as a string not just an array of characters. That is why you should substract one from string length which is reserved for nul terminator

0

Brother, you have decleared char array as char n[50]; which can store only maximum of 50 charecters as a string at 'n'. As you want to store 'a' strings; you would have to allocate memory for 'a' strings as char[a][50]; and then store the strings at respective memory locations using for loop. Hence, the program would be as:

#include<stdio.h>
#include<string.h>
int main()
{
   int a;   
   scanf("%d",&a);
   char n[a][50];
   for(int i=0;i<a;i++)
   {
      scanf("%s",n[i]);
   }

   for(int i=0;i<a;i++)
   {        
      printf("%s\n",n[i]);
   }
   return 0;
}

As each string can vary in size, this type of declearation may lead to loss of memory or loss of data. Hence, more efficient way to solve the problem would be to allocate memory for a input string and store it there. Once stored, we can use the string by just it's base address, hence we would just store the base addresses of every stored string in a array of pointers to char(string). Hence, char *n[a] ; would help us to store pointers to char. Hence the solution would be as :

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main( )
{
int len, i, a ;
char n[ 50 ] *p ;
scanf("%d", &a);
char *names[a] ;
for ( i = 0 ; i < a ; i++ )
{
printf ( "Enter string1 : " ) ;
scanf ( "%s", n ) ;
len = strlen ( n ) ;
p = ( char * ) malloc ( len + 1 ) ; /* +1 for accommodating \0 */
strcpy ( p, n ) ;
names[ i ] = p ;
}
for ( i = 0 ; i < a ; i++ )
printf ( "%s\n", names[ i ] ) ;
return 0 ;
}

In this program, we have first received a name using scanf( ) in a string n[ ]. Then we have found out its length using strlen( ) and allocated space for making a copy of this name. This memory allocation has been done using a standard library function called malloc( ). You may read more about malloc here. Then, we just copied the string to the memory location by using strcpy( ) library function in string.h and stored the address in the array.

We have used the same array to print the stored strings by their memory locations, in another for loop.

You may read all about this in the book Let Us C - by Yashwant Kanetkar in Chapter : Strings and Chapter : Handling Multiple Strings.

Note : Some compilers don't allow to declear variable after using library functions as we have used char[a][n] and char *names[a] ; after scanf(). To solve this, you would have to predefine the maximum number of strings to take as input or else you would have to modify the program and use dynamic memory allocation more efficiently. I am not posting it here as this would become a little more complex.