4.10
#include<iostream>
using namespace std;
int main()
{
char s[]="this is a progreamming test.";
int i=0,pLen=0,maxLen,pSeat=0;
while(s[i]!='\0')
{
while(s[i]!=' '&&s[i]!='\0')
{
pLen++;
i++;
}
if(pLen>maxLen)
{
pSeat=i-pLen;
maxLen=pLen;
}
while(s[i]==' ')
i++;
pLen=0;
}
cout<<"最长的单词为:";
for(i=0;i<maxLen;i++)
cout<<s[pSeat+i];
cout<<endl;
return 0;
4.13
#include<iostream>
using namespace std;
int main()
{
char str[50];
int len=0;
cout<<"请输入一串字符:";
cin.get(str,50);
while(str[len]!='\0')
len++;
cout<<"字符串"<<str<<"的长度为:"<<len<<endl;
return 0;
}
作业题1
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int a[5],i,j,k;
cout<<"请输入5个数:";
cin>>a[i];
cout<<"输入的5个数为:";
for(i=0;i<5;i++)
cout<<setw(4)<<a[i];
cout<<endl;
cout<<"按从大到小顺序输出:"<<endl;
for(i=1;i<5;i++)
for(j=4;j>i-1;j--)
{
if(a[j]>a[j-1])
{
k=a[j];
a[j]=a[j-1];
a[j-1]=k;
}
}
for(i=0;i<5;i++)
cout<<a[i]<<" ";
return 0;
}