Q. Does both the loops in the following programs prints the correct string length?

Code:
#include<iostream>

using namespace std;
main() {
   int i;
   
   char s[] = "hello";

   for(i=0; s[i]; ++i);
      cout<<i<<endl;

   i=0; 
   
   while(s[i++]);
      cout<<i;
}
  • (A) Yes, both the loops prints the correct length
  • (B) Only for loop prints the correct length
  • (C) Only while loop prints the correct length
  • (D) Compile error in the program.
πŸ’¬ Discuss
βœ… Correct Answer: (B) Only for loop prints the correct length

You must be Logged in to update hint/solution

πŸ’¬ Discussion


πŸ“Š Question Analytics

πŸ‘οΈ
294
Total Visits
πŸ“½οΈ
4 y ago
Published
πŸŽ–οΈ
Ram Sharma
Publisher
πŸ“ˆ
91%
Success Rate