新聞中心
c語言 將輸入的字符串按照空格分割
strtok函數(shù)
創(chuàng)新互聯(lián)公司專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站制作、網(wǎng)站設(shè)計、外貿(mào)網(wǎng)站建設(shè)、潞城網(wǎng)絡(luò)推廣、微信小程序定制開發(fā)、潞城網(wǎng)絡(luò)營銷、潞城企業(yè)策劃、潞城品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)公司為所有大學(xué)生創(chuàng)業(yè)者提供潞城建站搭建服務(wù),24小時服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
網(wǎng)頁鏈接
char str[] ="i love c love c";
const char * split = " ";
char * p;
p = strtok (str,split);
while(p!=NULL) {
printf ("%s\n",p);
p = strtok(NULL,split);
}
這么循環(huán)
說下我的邏輯,不一定最優(yōu)
先弄個結(jié)構(gòu)體struct里面有一個char*和一個int
再建個struct的數(shù)組
在每次循環(huán)對比之前獲得的struct數(shù)組中是否含有相同的字符串
有就計數(shù)器+1
沒有就在數(shù)組中為null的地方加上一個成員為這個字符串和計數(shù)為1的struct
循環(huán)結(jié)束時遍歷struct數(shù)組
好久沒寫C了,怕給你的代碼有bug,就寫思路把
C語言如何讀取一行數(shù)據(jù),以空格分開
可以使用strtok函數(shù)做分割單詞。
#includestring.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("\n");
}
擴展資料
在C++中strtok的使用
#includeiostream
#includecstring
usingnamespacestd;
intmain()
{
charsentence[]="Thisisasentencewith7tokens";
cout"Thestringtobetokenizedis:\n"sentence"\n\nThetokensare:\n\n";
char*tokenPtr=strtok(sentence,"");
while(tokenPtr!=NULL){
couttokenPtrendl;
tokenPtr=strtok(NULL,"");
}
//cout"Afterstrtok,sentence="tokenPtrendl;
return0;
}
C語言中輸入字符串,里面有空格,怎么根據(jù)空格把字符串分開,并存在數(shù)組里?
程序源碼如下:
#includestdio.h
#includestring.h
int?main(void)
{
char str[1000];//定義一個字符串?dāng)?shù)組
char strnew[1000];//定義一個備用字符串?dāng)?shù)組
char m[]?=?"?";//定義空格變量
printf("請輸入一串字符:");//文字提示輸入字符串
gets(str);//輸入字符串
char?*p?=?strtok(str,m);//取str與m的指針
printf("%s\n",p);? //輸出
p?=?strtok(NULL,m);
while(p)? //遍歷輸出
{ ? ?
printf("%s\n",p); //輸出字符串
p?=?strtok(NULL,m);? //指向下一個
}
}
程序輸出結(jié)果:
擴展資料:
C語言:輸入一個字符串放入數(shù)組里,刪除其中的空格
#include stdio.h
#includestring.h
#define N 100
void main() ? ? ? ? ? ? ? ? ??
{
int i=0,j;
char c,str[N];
printf("輸入字符串str:\n");
while((c=getchar())!='\n')
{
str[i]=c;//輸入字符串
i++;
}
str[i]='\0';
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' ')
{
for(j=i+1;str[j]!='\0';j++)
{
str[j-1]=str[j]; ? ?
}
str[j]='\0';
}
else continue;
}
str[i-2]='\0';
printf("去掉空格后的字符串為:\n");
for(i=0;str[i]!='\0';i++)
printf("%c",str[i]);
printf("\n");
}
當(dāng)前名稱:c語言按空格拆分函數(shù) c語言用空格隔開輸出一串?dāng)?shù)字
網(wǎng)頁路徑:http://www.ef60e0e.cn/article/dosdhsc.html