Reputation: 31
#include<stdio.h>
int main()
{
char arr[100], i;
int n;
printf("Enter the size of the Array: ");
scanf("%d", &n);
printf("\nEnter the Strings in the Array\n");
for(i=0; i<n; i++)
{
scanf("%s", &arr[i]);
}
for(i=0; i<n; i++)
{
printf("%s", arr[i]);
}
return 0;
}
Why does the execution of this code just stop after taking the input of string variables? Why isn't it printing the string in array?
Upvotes: 3
Views: 151
Reputation: 100
According to your code, Are you trying to read an array of characters.
For reading a single character in C we should use the %c
specifier.
Suppose n=5
we can have 4 characters and implicit added '\0'
which makes a string.
#include <stdio.h>
#include<stdio.h>
int main()
{
char arr[100], i;
int n;
printf("Enter the size of the Array: ");
scanf("%d", &n);
printf("\nEnter the Strings in the Array\n");
for(i=0; i<n; i++)
{
scanf("%c", arr[i]);
}
for(i=0; i<n; i++)
{
printf("%c", arr[i]);
}
return 0;
}
Or an array of strings example.
#include <string.h>
#include <stdio.h>
#define NUM_STRINGS 10
int main(){
char *arr3[NUM_STRINGS] = { "first string",
"second string",
"third string",
"fourth string",
"fifth string" };
for (int i = 0; i < NUM_STRINGS; ++i) {
printf("%s, ", arr3[i]);
}
printf("\n");
}
Upvotes: 1
Reputation: 9733
You are overwriting the same string every time but starting one deeper in the array on every iteration. You can see the problem more easily when the array has a size of 4.
char arr[4], i;
int n = 4;
printf("\nEnter the Strings in the Array\n");
for(i=0; i<n; i++)
{
scanf("%s", &arr[i]);
}
Now the first time when you enter a string more than 3 characters your application will fault. The second time more than 2 characters because you start in the same string +1 byte. The address of arr + 1.
The third time only 1 bytes is still valid, etc.
Change your array into something like char arr[100][100]; Then you have 100 char arrays of array of 100 big. Also remove ampersand from the scanf as at that point arr[i] already is an address.
scanf("%s", arr[i]);
Same as:
scanf("%s", &arr[i][0]);
Upvotes: 4
Reputation: 24846
The line
char arr[100];
will only allocate space for a single string of up to 100 characters (99 characters plus the terminating null character). If you want to allocate space for 100 strings of 100 characters each, you should write this instead:
char arr[100][100];
Another problem is that using scanf
with the %s
conversion format specifier will only read a single word, not a whole line of input. If you want to read a whole line of input instead, you should use the fgets
function instead.
Here is an example program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_STRINGS 100
#define MAX_STRING_LENGTH 100
int main( void )
{
char strings[MAX_STRINGS][MAX_STRING_LENGTH];
int n;
int c;
//read number of strings
printf( "Enter the number of strings: " );
if ( scanf( "%d", &n ) != 1 )
{
printf( "input error!\n" );
exit( EXIT_FAILURE );
}
//verify that number of strings is acceptable value
if ( ! ( 0 <= n && n <= MAX_STRINGS ) )
{
printf( "Input must be between 0 and %d.\n", MAX_STRINGS );
exit( EXIT_FAILURE );
}
//discard remainder of input line
do
{
c = getchar();
}
while ( c != EOF && c != '\n' );
//input all strings
for ( int i = 0; i < n; i++ )
{
//prompt user for input
printf( "Enter string #%d: ", i + 1 );
//attempt to read one line of input
if ( fgets( strings[i], sizeof *strings, stdin ) == NULL )
{
printf( "input error!\n" );
exit( EXIT_FAILURE );
}
//remove newline character from input
strings[i][strcspn(strings[i],"\n")] = '\0';
}
//output all strings
printf( "\nOutput:\n\n" );
for( int i = 0; i < n; i++ )
{
printf( "%s\n", strings[i] );
}
}
This program has the following behavior:
Enter the number of strings: 3
Enter string #1: This is a test.
Enter string #2: This is another test.
Enter string #3: This is yet another test.
Output:
This is a test.
This is another test.
This is yet another test.
Upvotes: 1
Reputation:
#include<stdio.h>
int main(){
//stores the size of the array
int n;
printf("Enter the size of the Array: ");
scanf("%d", &n);
//n represents number of strings you want to store
char arr[n][100], i;
printf("\nEnter the Strings in the Array\n\n");
for(i=0; i<n; i++){
scanf("%s", arr[i]);
}
printf("\nOutput:\n\n");
for(i=0; i<n; i++){
printf("%s\n", arr[i]);
}
return 0;
}
$ gcc array.c && ./a.out
Enter the size of the Array: 3
Enter the Strings in the Array
first
second
third
Output:
first
second
third
#include<stdio.h>
int main(){
//stores the size of the array
int n;
printf("Enter the size of the Array: ");
scanf("%d", &n);
//n represents number of strings you want to store
char arr[n][100], i;
printf("\nEnter the Strings in the Array\n\n");
for(i=0; i<n; i++){
//flushs the previous buffer
getchar();
//It will read spaces now
scanf("%[^\n]s", arr[i]);
}
printf("\nOutput:\n\n");
for(i=0; i<n; i++){
printf("%s\n", arr[i]);
}
return 0;
}
$ gcc array.c && ./a.out
Enter the size of the Array: 3
Enter the Strings in the Array
first string
second
third string...
Output:
first string
second
third string...
Upvotes: 1