0
Why this code output "no output" in C?
Please note that I try this code to reverse the string..... But,i don't know about working of for loops in this code.. Please help to understand this code https://code.sololearn.com/cNh47ZDSyG4J/?ref=app
9 Antworten
+ 5
It works that in each lap of the outer loop, you run the inner loop from the start till its exit condition is met.
If you want to visualize it, add
printf("%d %d\n", i, j);
In the inner loop.
You will see that the i is blocked to the same value (meaning we are in the same lap of the outer loop) for all the duration of the inner loop.
+ 6
You can't determine the length of the string before it has been entered by the user, so you need to call strlen() after fgets(), or else "len" will not yield the correct length.
You also don't need a nested for loop, since you only iterate over a linear array and copy the character to the second array.
+ 5
You don't need a double loop.
Delete the inner loop and do:
b[len-1-i] = a[i];
Or if you want do like that:
for(int i = 0; i < len; i++){
b[i] = a[len-1-i];
}
+ 4
When in a double loop you do
b[j] = a[i];
In each lap of the outer loop you are filling all the subscripts of b[] with the same a[i] character
+ 4
You are welcome 🤗
+ 1
Thank you Davide for yours help in knowing about nested for loop behaviour....
I ALSO TRY THAT CODE BASED ON YOURS COMMENT .....IT WORKS VERY WELL....
https://code.sololearn.com/cNh47ZDSyG4J/?ref=app
+ 1
Davide 🙌
0
Thank you Shadow....
For knowing me to recognise my mistake😂...
Now I will rectify that....
But can you tell me, how does nested for loop work....?
i.e inner for loop vs outer loop...
and how can I reverse string using nested for loops....? is it possible?
0
Davide can you tell me...
How does nested for loop behaves?
I mean...
In which manner [order] nested for loop run...
For reference,considered nested for loop in my code