Member Avatar for mahavairavan

Hi all,
Can anyone please enhance the following code to print a string reverely ina consecutive manner using two dimensional array.

The program which i tried in codepad.org is follows:

#include<stdio.h> #include<string.h> int main() { char a[4][4]={'j','u','s','t'}; int i,l,j,k,b=4,n; printf("Enter the string"); printf("\n"); for(i=0;i<1;i++) { for(j=0;j<b;j++) { printf("%c",a[i][j]); } } printf("\n"); n=b; for(i=0;i<b-1;i==0) { for(k=n-1;k<b;k++) { printf("%c",a[i][k]); } for(j=0;j<n-1;j++) { printf("%c",a[i][j]); } printf("\n"); n--; } }

The output which i got was:
Enter the string
just
tjus
stju
ustj
just
�just
B�just
�B�just
0�B�just
@0�B�just
@0�B�just
X@0�B�just
(X@0�B�just
@(X@0�B�just
@(X@0�B�just
�@(X@0�B�just
��@(X@0�B�just
@��@(X@0�B�just
@��@(X@0�B�just
U@��@(X@0�B�just
�U@��@(X@0�B�just
�U@��@(X@0�B�just
�U@��@(X@0�B�just
�U@��@(X@0�B�just


The output should be
eg if the input string is just it should print
just
tjus
stju
ustj
The program print the above output but in the second i loop,since the i value is getting incremented it will print
just
tjus alone and gets terminated.So for that i made the loop not to increment but it results in the folloing out put.please rewrite this program to accept input dynamically many strings and reverse it as i showed the output in second please.

Member Avatar for WaltP

Start by formatting the code so we can follow it.

Member Avatar for Tumlee

In the comments you'll see some potential problems with this code.

#include<stdio.h> #include<string.h> int main() { //You're intializing a two dimensional array with a one-dimensional initializer. //This may cause problems. char a[4][4]={'j','u','s','t'}; int i,l,j,k,b=4,n; //If b is the number of characters, why not name it numchars or something like that? //Asking the user to enter a string and not giving him a chance to input. printf("Enter the string"); printf("\n"); //What's the point of this loop? It only executes once while i is zero. for(i=0;i<1;i++) { for(j=0;j<b;j++) { printf("%c",a[i][j]); } } printf("\n"); n = b; //Huh? i==0? This is probably your problem. i is never changing in this loop at all. //b also never changes, so your loop will go on forever. for(i=0;i<b-1;i==0) { //Even if i was properly incrementing, you never set all 16 the values in the //array. You'll be reading from uninitialized garbage when i is greater than zero. for(k=n-1;k<b;k++) { printf("%c",a[i][k]); } for(j=0;j<n-1;j++) { printf("%c",a[i][j]); } printf("\n"); //Because your loop is infinite, n becomes a negative number, forcing this value //into k will force your program to read a negative index in the array, crashing //your program. n--; } //ALL programs should have a return statement at the end of main(). //In most cases, you want to return 0; }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.