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++
9 Réponses
+ 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.
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?
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;
}
0
Thank u so much
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?
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;
}
0
Thank u so much sir
0
Your welcome