char a[1000] = "welcome to china beijing is the capital of china you isnot a true man if didnot went to changcheng enjoy your journey";
printf("%s\n",a);
char b[100][20] = {0}; // 10太小,改成20
// 定义变量保存单词个数
int count = 0;
// 循环遍历数组a
for (int k = 0, j = 0; k < 1000; k++) {
// 如果是空格
if (a[k] == ' ') {
// 添加结束符
b[count][j] = 0;
// 单词输+1
count++;
// 下一个单词从0角标开始保存
j = 0;
// 跳过本次循环
continue;
}
// 保存当前字符
b[count][j] = a[k];
// 字符串结束
if (a[k] == '\0') {
// 添加结束符
b[count][j] = 0;
// 跳出循环
break;
}
// 角标后移一位
j++;
}
// 循环打印每个单词,只打印保存过的
for(int j=0;j <= count;j++)
{
// 打印单词
printf("%s ",b[j]);
}
printf("\n");
return 0;
}
复制代码
作者: 又召 时间: 2014-4-21 00:01
#include <stdio.h>
#include <string.h>
int main ()
{
char a[1000] = "welcome to china beijing is the capital of china you isnot a true man if didnot went to changcheng enjoy your journey";
printf("%s\n",a);
// for(int k = 0,k < 1000,k++)
// {
// char b [i][j] = char a [k];
// }
int k = 0;
char b[100][10];
//for(int k = 0;k < 1000;k++)
//{
for(int i = 0;i<100;i++)
{
for(int j =0;j<10;j++)
{
if(a[k]!='\0')
{
b[i][j] = a[k];
k++;
}
}