arrays - printing an string in cross manner using c program -
i'am trying write program converting given string in cross manner(i.e diagonal left-right , right-left). if string length returns message else arrange in cross form.
the code is:
#include<stdio.h> #include<string.h> int main() { char str[50]; char str2[50][50]; int lenstr; int i,j; char temp; printf("enter string :\n"); scanf("%s",str); lenstr = strlen(str); if(lenstr %2 == 0) { printf("the string length must odd length"); } else { j = 0; temp = 0; for(i = 0;i == lenstr;i++) { str2[i][j] = str[i]; j = j + 1; } for(i = lenstr; i==0 ;i--) { j = lenstr; str2[i][j] = str[temp]; temp = temp + 1; j = j - 1; } for(i = 0;i<lenstr;i++) { for(j = 0;j<lenstr;j++) { printf("%c",str2[i][j]); } printf("\n"); } } return 0; }
the output program must example: geeks
g g e e e k k s s
but output obtained consists of different shapes(like heart,smiley face etc...). explain concept behind correct , if can please explain when using pointers same program. appreciated.
in code, need change for
loop condition checking expressions, i == lenstr
, later i==0
. not entering loop, essentially.
instead, can replace whole block
for(i = 0;i == lenstr;i++) { str2[i][j] = str[i]; j = j + 1; } for(i = lenstr; i==0 ;i--) { j = lenstr; str2[i][j] = str[temp]; temp = temp + 1; j = j - 1; }
in code by
for(i = 0;i<lenstr;i++) for(j = 0;j<lenstr;j++) str2[i][j] = ' '; //fill 2d array space for(i = 0;i < lenstr;i++) { str2[i][i] = str[i]; //set character str2[i][lenstr- -1] = str[i]; //reverse position }
and desired output.
see live.
Comments
Post a Comment