#include
#include
using namespace std;
int main()
{
char s[1000];
gets(s);
int n = strlen(s);
for (int i = 0; i < n; i++)
{
int j = i;
while (j < n && s[j] != ' ')
{
j++;
}
for(int k = i; k < j; k++)
{
cout<<s[k];
}
cout<<endl;
}
}
1.gets函数
2.strlen函数
#include
#include
using namespace std;
int main()
{
char str[20];
cout<<"请输入一个字符串:";
gets(str);
cout<<"您输入的字符串为:"<<str;
}
unsigned int strlen(const char *str)
{
assert(str != Null);
unsigned int len = 0;
while (*str++)
{
len++;
}
return len;
}
#include
using namespace std;
int main()
{
char str[] = "hello,world";
char name[20] = "Jenny";
char *ch = "hello";
int len1 = strlen(str);
int len2 = strlen(name);
int len3 = strlen(ch);
cout << "strlen:字符串长度 " << endl;
cout << "str: " << len1 << endl;
cout << "name: " << len2 << endl;
cout << "ch: " << len3 << endl;
cout << "sizeof: 内存字节数" << endl;
cout << "str: " << sizeof(str) << endl;//这里对字符串末尾的\0是占用内存字节的,所以用sizeof求字节数比字符串长度大1.
cout << "name: " << sizeof(name) << endl;
cout << "ch: " << sizeof(ch) << endl;//因为ch是指针,指针是地址,是占用32位,也就是4个字节。与指针是何类型无关
return 0;
}