字符串,二维数组(输入文本,统计文本单词个数)
include <iostream>
using namespace std;
int strlen(char *s)
{ char *p=s;
int count=1;
while(*p!='\0')
{
p++;
if (*p==' '&&*(p+1)!=' '&&*(p+1)!='\0')
count++;
}
return count;
}
int main()
{
char str[50];
cout<<"please input a string:";
cin.get(str,50);
cout<<str<<endl;
cout<<"the word have"<<" "<<"count:";
cout<<strlen(str)<<endl;
return 0;
}
5幻方代码
#include <iostream>
#include <iomanip>
using namespace std;
const int n=5;
int main()
{
int a[n][n];
int i;
int j;
int k;
int itemp;
int jtemp;
i=0;
j=n/2;
a[i][2]=1;
for(k=2;k<=n*n;k++)
{
itemp=i;jtemp=j;
i=(i-1+n)%n;
j=(j-1+n)%n;
if(a[i][j]>0)
{
i=(itemp+1)%n;
j=jtemp;
}
a[i][j]=k;
}
cout<<"五幻方为:"<<endl;
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
cout<<setw(5)<<a[i][j];
cout<<endl;
}
return 0;
}