0

Can you help me in this problem...Write a c++ program that accepts 5 words and print the longest word ...(using array And loops)

C++

11th Nov 2020, 3:08 AM
Reymart Selverio
Reymart Selverio - avatar
8 Respuestas
+ 1
First get the 5 string input using array and for loop ,get the length of all strings ,compare length using for loop and store longest word . This is the logic now it's your turn first write the code in c++, if you got any problem than ask the question.
11th Nov 2020, 3:15 AM
Prathvi
Prathvi - avatar
0
#include <iostream> #include<string> #include<algorithm> using namespace std; int main() { string names[5]; cout<<"Enter 5 Names"<<endl; for(int c = 0; c<5; c++) { getline(cin,names[c]); } What's next?
11th Nov 2020, 3:50 AM
Reymart Selverio
Reymart Selverio - avatar
0
#include <iostream> #include<cstring> using namespace std; int main() { string names[5],s; int l[5],m; cout<<"Enter 5 Names"<<endl; for(int c = 0; c<5; c++) { cin>>names[c]; l[c]=names[c].length(); } int max=l[0]; for(int c = 1; c<5; c++) { if(l[c]>max){ max=l[c]; m=c; } } cout<<names[m]; return 0; }
11th Nov 2020, 4:33 AM
Prathvi
Prathvi - avatar
0
Thank u so much
11th Nov 2020, 4:34 AM
Reymart Selverio
Reymart Selverio - avatar
0
Sir..your code only outputs the value of 1-4 in the array... How about the value of 0 if that is the longest word?
11th Nov 2020, 6:43 AM
Reymart Selverio
Reymart Selverio - avatar
0
#include <iostream> #include<cstring> using namespace std; int main() { string names[5],s; int l[5],m; cout<<"Enter 5 Names"<<endl; for(int c = 0; c<5; c++) { cin>>names[c]; l[c]=names[c].length(); } int max=l[0]; m=0; for(int c = 1; c<5; c++) { if(l[c]>max){ max=l[c]; m=c; } } cout<<names[m]; return 0; }
11th Nov 2020, 6:50 AM
Prathvi
Prathvi - avatar
0
Thank u so much sir
11th Nov 2020, 6:57 AM
Reymart Selverio
Reymart Selverio - avatar
0
Your welcome
11th Nov 2020, 6:59 AM
Prathvi
Prathvi - avatar