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
12 ответов
+ 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
+ 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
+ 1
by the way, thanks for solve this problem
+ 1
Can you explain how nested for for loop here and -j logic work here?Mihail
+ 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
+ 1
Hello i need little help
+ 1
May I know what help you needResQ Hustler ?
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");
}
0
not work, the world change to wor not worl
0
can you explain this logic? why like that?
0
You doesn't even need string.h header file.
https://code.sololearn.com/c1uO8NjQYBrD/?ref=app
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");
}