c语言空格分割字符串
㈠ c语言如何读取一行数据,以空格分开
可以使用strtok函数行庆清做分割单词。
#include<string.h>
voidmain()
{
chars[]="192.168.0.26";
char*delim=".";
char*p;
printf("%s",strtok(s,delim));
while((p=strtok(NULL,delim)))
printf("%s",p);
printf(" ");
}
(1)c语言空格分割字符串扩展阅读
在C++中strtok的使用
#include<iostream>
#include<cstring>
usingnamespacestd;
intmain()
{
charsentence[]="Thisisasentencewith7tokens";
cout<<差枝"Thestringtobetokenizedis: "<<sentence<<" Thetokensare: ";
char*tokenPtr=strtok(sentence,"");
while(tokenPtr!=NULL){
cout<<tokenPtr<<endl;
tokenPtr=strtok(NULL,"");
}
//cout<<"Afterstrtok,sentence="<<tokenPtr<<档前endl;
return0;
}
㈡ C语言中输入字符串,里面有空格,怎么根据空格把字符串分开,并存在数组里
程序源码如下:
#include<stdio.h>
#include<string.h>
intmain(void)
{
char str[1000];//定义一个字符串数组
char strnew[1000];//定义一个备用字符串数组
char m[]="";//定义空格变量
printf("请输入一串字符:");//文字提示输入字符串
gets(str);//输入字符串
char*p=strtok(str,m);//取str与m的指针
printf("%s ",p); //输出
p=strtok(NULL,m);
while(p) //遍历输出
{
printf("%s ",p); //输出字符串
p=strtok(NULL,m); //指向下一个
}
}
程序输出结果:
(2)c语言空格分割字符串扩展阅读:
C语言:输入一个字符串放入数组里,删除其中的空格
#include <stdio.h>
#include<string.h>
#define N 100
void main()
{
int i=0,j;
char c,str[N];
printf("输入字符串str:
");
while((c=getchar())!='
')
{
str[i]=c;//输入字符串
i++;
}
str[i]='