0

HELP ME SOLVE THIS C PROGRAMMING EXAM (SOLVED)

tell me how to program C to produce an output like this, using the string strlen function: WORLD WORL WOR WO W why my code doesn't work? https://code.sololearn.com/c1aBk5DAn0TA/?ref=app

28th Apr 2021, 7:17 PM
hafidz ridwan cahya
hafidz ridwan cahya - avatar
12 Answers
+ 8
Mihail In your logic there should be -j in second loop. hafidz ridwan cahya See working code here https://code.sololearn.com/cJwF89aGgWSx/?ref=app
28th Apr 2021, 7:43 PM
A͢J
A͢J - avatar
+ 2
hafidz ridwan cahya In second loop after every iteration of 1st loop, size will reduce because of strlen(str) - j So when j = 0 then 2nd loop will iterate 5 times and print WORLD When j = 1 then 2nd loop will iterate 4 times and print WORL -------------------- -------------------- When j = 4 then 2nd loop will iterate 1 times and print W
28th Apr 2021, 7:54 PM
A͢J
A͢J - avatar
+ 1
by the way, thanks for solve this problem
28th Apr 2021, 7:45 PM
hafidz ridwan cahya
hafidz ridwan cahya - avatar
+ 1
Can you explain how nested for for loop here and -j logic work here?Mihail
29th Apr 2021, 2:26 AM
Hemasri Kottapalli
Hemasri Kottapalli - avatar
+ 1
since it doesn't ask to preserve the content, I would have solved it like that #include <stdio.h> #include <string.h> int main() { char arr[300]; scanf("%s", arr); for(int l=strlen(arr); l; arr[--l]='\0') printf("%s\n",arr); return 0; } https://code.sololearn.com/c66gsZCt5aU5/?ref=app if you want to preserve the string you can do so. two nested for loops with continuous calls to strlen are wasteful #include <stdio.h> #include <string.h> int main() { char arr[300]; scanf("%s", arr); char t='\0'; for(int l=strlen(arr); l; arr[--l]='\0'){ printf("%s\n",arr); arr[l]=t; t=arr[l-1]; } arr[0]=t; printf("%s\n",arr); return 0; } https://code.sololearn.com/ccP90RutFoSW/?ref=app
29th Apr 2021, 8:10 PM
Ciro Pellegrino
Ciro Pellegrino - avatar
+ 1
Hello i need little help
30th Apr 2021, 1:23 PM
ResQ Hustler
ResQ Hustler - avatar
+ 1
May I know what help you needResQ Hustler ?
30th Apr 2021, 1:28 PM
Hemasri Kottapalli
Hemasri Kottapalli - avatar
0
if you have a char array it would go something like this: int i,j; for(j=0;j<strlen(arr);j++) { for(i=0;i<strlen(arr)-i;i++) { printf("%c",arr[i]); } printf("\n"); }
28th Apr 2021, 7:25 PM
Mihail
Mihail - avatar
0
not work, the world change to wor not worl
28th Apr 2021, 7:33 PM
hafidz ridwan cahya
hafidz ridwan cahya - avatar
0
can you explain this logic? why like that?
28th Apr 2021, 7:45 PM
hafidz ridwan cahya
hafidz ridwan cahya - avatar
0
You doesn't even need string.h header file. https://code.sololearn.com/c1uO8NjQYBrD/?ref=app
30th Apr 2021, 12:19 AM
🌀 Shail Murtaza شعیل مرتضیٰ
🌀 Shail Murtaza شعیل مرتضیٰ - avatar
0
Sorry, I made a mistake in the second for loop. I fixed it now. int i,j; for(j=0;j<strlen(arr);j++) { for(i=0;i<strlen(arr)-j;i++) { printf("%c",arr[i]); } printf("\n"); }
30th Apr 2021, 4:35 PM
Mihail
Mihail - avatar