2-dimensional array on line creating erroneous output in C
I am trying to do something as simple as printing the reverse string. EXAMPLE:
Hello World! This is me
O / P required:
me is This World! Hello
My code looks something like this:
#include<stdio.h>
#include<string.h>
int main(){
char *arr[20] ;
int i,j;
int size;
char *revarr[20];
printf(" enter the number of words\n");
scanf("%d",&size);
for(i=0;i<size;i++)
scanf("%s",&arr[i]);
for(i=0;i<size;i++)
{
printf("%s\n",&arr[size-1-i]); //overwritten words
revarr[i]=arr[size-1-i];
}
printf(" the reversed sentence is %s\n",(char *)revarr);
}
I other than arr [0], arr [1], etc. to be separate objects, but when printed and stored, they seem to overlap like this: i / p:
Hello World
o / r
World
HellWorld
the reversed sentence is WorlHell@#$
I can't figure out what's wrong! Thanks in advance!
EDIT: When printing
printf(&arr[0]);
printf(&arr[1]);
I get:
HellWorld
World
I expected it to be printed
Hello
World
source to share
You declared arr
and revarr
as an array of pointers char
. You need to dynamically allocate memory for your elements.
Also note that you don't need &
in operators
scanf("%s",&arr[i]);
and
printf("%s\n", &arr[size-1-i]);
// ^No need of &
Here is a modified version of your code. Note that there is no need to use revarr
to change the line.
#include<stdio.h>
#include<stdlib.h>
int main(){
size_t i, size;
printf("Enter the number of words\n");
scanf("%d", &size);
char *arr[size] ; // Variable length array. Supported by C99 and latter
for(i = 0; i < size; i++)
{
arr[i] = malloc(20); // Assumimg words are no longer than 20 characters
scanf("%s", arr[i]);
}
printf("The reversed sentence is:\n");
for(i = size-1; i >= 0; i--) // Run loop in reverse order and print words
printf("%s ", arr[i]);
}
source to share
You didn't allocate memory for arr[0], arr[1],
etc before using them to read lines into
scanf("%s",&arr[i]);
This is the reason for undefined behavior. You need something like:
int main(){
char *arr[20] ;
int i,j;
int size;
char *revarr[20];
printf(" enter the number of words\n");
scanf("%d",&size);
for(i=0;i<size;i++)
{
// Allocate memory.
// make it large enough to hold the input
arr[i] = malloc(100);
scanf("%s", arr[i]);
}
for(i=0;i<size;i++)
{
revarr[i]=arr[size-1-i];
}
printf(" the reversed sentence is: ");
for(i=0;i<size;i++)
{
printf("%s ", revarr[i]);
}
printf("\n");
// Deallocate the memory.
for(i=0;i<size;i++)
{
free(arr[i]);
}
return 0;
}
source to share
This is a good approach to your problem:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
/* the string line will contain the line of the input */
char line[100];
/* read the string with the function f gets */
fgets(line,100,stdin);
/* the tab will contain all the string of the variable line */
char *tab[20];
/* the variable p will point to each string of the line */
char *p=NULL;
/* we extract the strings of the line via the function strtok */
p=strtok(line," ");
int nb=-1;
while (p!=NULL)
{
nb++;
/* we allocate a space memory fo every str ing */
tab[nb]=malloc(sizeof(char)*100);
strcpy(tab[nb],p);
p=strtok(NULL," ");
}
/* there is an exception with the last string of the line we need to take care o f it */
tab[nb][strlen(tab[nb])-1]='\0';
int i;
/* print the strings in reverse or der */
for (i=nb;i>=0;i--)
{
printf("%s ",tab[i]);
/* dont forget to free the space memory at the end of the prog ram */
free(tab[i]);
}
printf("\n");
return 0;
}
source to share
You will need the following
#include <stdio.h>
#include <string.h>
int main(void)
{
size_t size;
printf( "enter the number of words: " );
scanf( "%zu", &size );
char arr[size][20];
char revarr[size][20];
for ( size_t i = 0; i < size; i++ ) scanf( "%s", arr[i] );
printf( "\n" );
for ( size_t i = 0; i < size; i++ ) strcpy( revarr[i], arr[size-i-1] );
printf( "the reversed sentence is" );
for ( size_t i = 0; i < size; i++ ) printf( " %s", revarr[i] );
printf( "\n" );
return 0;
}
If you enter
2
Hello World
then the output will be
World Hello
Note that the code will only compile if your compiler supports C99. Otherwise, you must dynamically allocate memory for character arrays.
As far as your code is concerned, it has undefined behavior and is generally invalid. You didn't allocate memory for each element of the arrays arr
and revarr
. You cannot assign one array to another. You should use a standard function strcpy
, etc. instead .
source to share