22 Ağustos 2015 Cumartesi

Determining The Largest Word In A String C++

#include
#include
using namespace std;

int main(){
//initialize the variables
string str;
int endword,arraysize;
int maxword=0;
int counter=0;
//getline command getting all line including spaces...
getline(cin,str);
//array size
arraysize = str.length();
//adding one "space" character at the end of string array for ending the procedure
str[arraysize+1]=' ';
//for loop will determine the longest character number
for(int i=0; i
if(str[i]!= ' '){

counter++;

}
else if(str[i] == ' '){
//once we have the whole world, compares the longest character number with the next one
if(counter>maxword){
maxword = counter;
//this is going to be used for counting characters in order to output the whole longest word
endword=i;

}

counter = 0;
}

}
//with the help of "endword" the location of characters which
//the longest word does have were known && we can get all characters together
for(int i=endword-maxword;i<=endword;i++)
cout< cout<<" the length is "< }


Hiç yorum yok:

Yorum Gönder

Yorumunuz...