主题:C++问题求助
Description
找出一个英文字符串中第一个最长的英文单词,字符串中的各英文单词以一个或多个空格分隔或标点符号分隔。如“I am a student.”最长英文单词为”student”。
Input
输入一串字符串。(字符个数不超过500,含标点符号和英文字母,最长英文单词的字符数不超过20)
Output
最长单词,占一行;
Sample Input
I am lucky.student went to school.
Sample Output
student
》》》》》》》》》》》》》》》》》》》》》》》》》
#include <iostream>
#include <string>
using namespace std;
int main()
{
char Sentence[501];
cin>>Sentence;
char *p,*p_temp;
int count,count_temp;
p_temp=Sentence;
count_temp=count=0;
for(int i=0;i<strlen(Sentence);i++)
{
if((Sentence[i]>='a'&&'z'>=Sentence[i])||(Sentence[i]>='A'&&'Z'>=Sentence[i]))
count_temp++;
else
{
if(count_temp>count)
{
count=count_temp;
p=p_temp-count_temp;
}
count_temp=0;
}
p_temp++;
}
while((*p>='a'&&'z'>=*p)||(*p>='A'&&'Z'>=*p))
{
cout<< *p;
p++;
}
cout<<endl;
system("pause");
return 0;
}
》》》》》》》》》》》》》》》
错在哪里呢?
找出一个英文字符串中第一个最长的英文单词,字符串中的各英文单词以一个或多个空格分隔或标点符号分隔。如“I am a student.”最长英文单词为”student”。
Input
输入一串字符串。(字符个数不超过500,含标点符号和英文字母,最长英文单词的字符数不超过20)
Output
最长单词,占一行;
Sample Input
I am lucky.student went to school.
Sample Output
student
》》》》》》》》》》》》》》》》》》》》》》》》》
#include <iostream>
#include <string>
using namespace std;
int main()
{
char Sentence[501];
cin>>Sentence;
char *p,*p_temp;
int count,count_temp;
p_temp=Sentence;
count_temp=count=0;
for(int i=0;i<strlen(Sentence);i++)
{
if((Sentence[i]>='a'&&'z'>=Sentence[i])||(Sentence[i]>='A'&&'Z'>=Sentence[i]))
count_temp++;
else
{
if(count_temp>count)
{
count=count_temp;
p=p_temp-count_temp;
}
count_temp=0;
}
p_temp++;
}
while((*p>='a'&&'z'>=*p)||(*p>='A'&&'Z'>=*p))
{
cout<< *p;
p++;
}
cout<<endl;
system("pause");
return 0;
}
》》》》》》》》》》》》》》》
错在哪里呢?