C语言以逗号分割字符串 C语言如何以逗号分割字符串 就是用scanf输入一行“1,2...
c\u8bed\u8a00\uff0c\u5982\u4f55\u8bfb\u53d6\u9017\u53f7\u5206\u9694\u7684\u5b57\u7b26\u4e32\uff0c\u5c06\u9017\u53f7\u95f4\u7684\u5b57\u7b26\u4e32\u5206\u522b\u63d0\u53d6\u51fa\u6765\uff1f\u5148\u5c06\u6240\u6709\u7684\u8bfb\u8fdb\u6765\u5b58\u5728\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\uff0c\u7136\u540e\u7528\u5b57\u7b26\u5206\u5272\u51fd\u6570strtok()//\u5177\u4f53\u53ef\u53c2\u89c1API
\u4f8b\u5982:
char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
\u4ee5\u4e0a\u4ee3\u7801\u7684\u8fd0\u884c\u7ed3\u679c\u662f\uff1a
result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"
int a[4];
scanf("%d,%d,%d,%d", &a[0], &a[1], &a[2], &a[3]);
#include<iostream>
#include<vector>
#include<sstream>
usingnamespacestd;
intmain()
{
strings;
vector<int>v;
cin>>s;
//将读入的字符串转化成is流
istringstreamis(s);
intinter;
charch;
while(is>>inter)//只能读出is流中的一个整形读进inter
{
v.push_back(inter);
is>>ch;//然后读一个字符型读进ch
}
for(inti=0;i<v.size();i++)
cout<<v[i]<<"";
cout<<endl;
return0;
}
扩展资料
C语言的字符串按照指定字符串分割操作
#include<stdio.h>
#pragmawarning(disable:4996)
#include<stdlib.h>
intmain()
{
charstr[]="我,是,中国,程序员";
char*ptr;
char*p;
printf("开始前:str=%s
",str);
printf("开始分割:
");
ptr=strtok(str,",");
while(ptr!=NULL){
printf("ptr=%s
",ptr);
ptr=strtok(NULL,",");
}
getchar();
}
C语言中要分割以逗号分隔的各个子串可以使用strtok()函数,函数原型:
char* strtok (char* str,constchar* delimiters );
函数功能:
切割字符串,将str切分成一个个子串
函数参数:
str:在第一次被调用的时间str是传入需要被切割字符串的首地址;在后面调用的时间传入NULL。
delimiters:表示切割字符串(字符串中每个字符都会 当作分割符)。
函数返回值:
当s中的字符查找到末尾时,返回NULL;
如果查不到delimiter所标示的字符,则返回当前strtok的字符串的指针。
参考代码:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char test_str[] = "this,is,a,,,test";
char *ptr,*retptr;
int i=0;
ptr = test_str;
while ((retptr=strtok(ptr, ",")) != NULL) {
printf("substr[%d]:%s
", i++, retptr);
ptr = NULL;
}
return 0;
}
运行结果:
注意,strtok()函数非线程安全函数,在多线程环境下可以使用strtok_r()代替,函数原型:
char *strtok_r(char *str, const char *delim, char **saveptr);
使用方法类似,只需要多定义一个字符指针,在每次调用前作为参数传递其地址即可,参考代码:
#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
char test_str[] = "this,is,a,,,test";
char *ptr,*retptr,*savestr;
int i=0;
ptr = test_str;
while ((retptr=strtok(ptr, ",", &savestr)) != NULL) {
printf("substr[%d]:%s
", i++, retptr);
ptr = NULL;
}
return 0;
}
这个用一个指针在字符串内走一下,做一个比较就搞定了,就是一个循环问题
#include <stdio.h>
void getstring(char *);
int main(void)
{
char test[100];
getstring(test);
return 0;
}
void getstring(char *test)
{
int count = 0;
while((test[count] = getchar()) != ',' && test[count] != '
')
++count;
test[count] = '\0';
}
给你推荐一个函数:strtok
嗯,可以
绛旓細1 濡傛灉浣跨敤scanf杩涜鏍煎紡鍖栬緭鍏ワ紝鐢ㄩ楀彿闅斿紑涓嶉傜敤浜庡厛杈撳叆瀛楃涓鍚庤緭鍏ユ暟瀛楃殑鎯呭喌锛屼粎閫傜敤浜庢暟瀛楃殑闂撮殧锛屾垨鑰呭厛杈撳叆鏁板瓧鍚庤緭鍏ュ瓧绗︿覆銆備笅闈㈡槸鍏堣緭鍏ユ暟瀛楀悗杈撳叆瀛楃涓茬殑渚嬪瓙锛歩nclude <stdio.h>int main(){char s[100];int v;scanf("%d,%s",&v, s);//鍏堣緭鍏ユ暣鍨嬪彉閲弙锛屽啀杈撳叆瀛楃涓诧紝鍙互浣跨敤閫...
绛旓細鍙互锛屼絾鏄緭鍏ユ暟鎹殑鏃跺欎竴瀹氫篃瑕佸姞涓婇楀彿銆傜敤scanf()璇诲彇鏁版嵁鏃讹紝杈撳叆鐨勬牸寮忓繀椤诲拰scanf()鐨勬牸寮忔帶鍒跺瓧绗︿覆瀵瑰簲锛屾瘮濡傝锛1銆乻canf("%d,%d",&a,&b);鍥犱负涓や釜%d涔嬮棿鏈変竴涓楀彿锛屾墍浠ュ湪杈撳叆鏃朵袱涓暣鏁颁箣闂村彧鑳界敤閫楀彿鍒嗛殧锛屽鏋滅敤鍏跺畠绗﹀彿鍒嗛殧灏变細鍑洪敊锛堟瘮濡傝姝ゆ椂鐢ㄧ┖鏍笺佸洖杞﹀垎闅斿氨浼氬嚭閿欙級2銆乻can...
绛旓細濡傛灉鏄閫楀彿鍒嗛殧鐨锛屽彲浠ョ敤涓嬮潰鏂规硶杩涜鍒囧壊锛歩nclude <stdio.h>#include <string.h> // 灏唖tr瀛楃浠spl鍒嗗壊,瀛樹簬dst涓紝骞惰繑鍥炲瓙瀛楃涓鏁伴噺int split(char dst[][80], char* str, const char* spl){ int n = 0; char *result = NULL; result = strtok(str, spl); while( ...
绛旓細include<iostream> include<vector> include<sstream> usingnamespacestd;intmain(){ strings;vector<int>v;cin>>s;//灏嗚鍏ョ殑瀛楃涓杞寲鎴恑s娴 istringstreamis(s);intinter;charch;while(is>>inter)//鍙兘璇诲嚭is娴佷腑鐨勪竴涓暣褰㈣杩沬nter { v.push_back(inter);is>>ch;//鐒跺悗璇讳竴涓瓧绗﹀瀷璇...
绛旓細C璇█鍙ぇ澶氭暟璇█涓鏍凤紝鍏佽鐢ㄩ楀彿鍒嗛殧澹版槑璇彞涓殑鏍囪瘑绗﹀垪琛紝璇存槑杩欎簺杩愮畻绗︽槸鍚屼竴鍙橀噺绫诲瀷銆備緥濡傦細float Area,Height,Width;浣嗘湁浜涚▼搴忓憳鍠滄鎶婃爣璇嗙鍐欏湪涓嶅悓鐨勮涓娿傚锛歠loat Area,Height, Width;杩欐牱鍐欒嚦灏戞湁涓涓ソ澶勶紝灏辨槸鍙互鍦ㄦ瘡涓爣璇嗙鍚庤竟鍔犱笂娉ㄩ噴銆傚湪澹版槑鍙橀噺鐨勬椂鍊欙紝涔熷彲浠ョ洿鎺ョ粰鍙橀噺...
绛旓細缁撹鏄紝C璇█鐨剆canf鍑芥暟鍦ㄨ緭鍏ユ暟鎹椂锛岀‘瀹炲彲浠ヤ娇鐢ㄩ楀彿鏉ュ垎闅斾笉鍚岀殑鏁版嵁绫诲瀷锛屼絾杩欓渶瑕佺‘淇濊緭鍏ョ殑鏍煎紡涓巗canf鐨勬牸寮忔帶鍒瀛楃涓鍖归厤銆備緥濡傦紝褰撲綘浣跨敤scanf("%d,%d",&a,&b);鏃讹紝杈撳叆鐨勬暣鏁板繀椤鐢ㄩ楀彿鍒嗛殧锛岃屼笉鑳界敤绌烘牸鎴栧洖杞︺傜劧鑰岋紝瀵逛簬scanf("%d%d",&a,&b);杩欐牱鐨勬牸寮忥紝绌虹櫧瀛楃锛堢┖鏍笺佸洖杞...
绛旓細c璇█scanf杈撳叆鏃惰兘鐢ㄩ楀彿闅斿紑锛屼絾鏄緭鍏ユ暟鎹殑鏃跺欎竴瀹氫篃瑕佸姞涓婇楀彿銆傜敤scanf()璇诲彇鏁版嵁鏃讹紝杈撳叆鐨勬牸寮忓繀椤诲拰scanf()鐨勬牸寮忔帶鍒瀛楃涓瀵瑰簲銆傚浜庢爣鍑嗚緭鍏ュ嚱鏁皊canf鏉ヨ锛屾嫭鍙峰唴鐨勬牸寮忓瓧绗︿覆锛屼篃灏辨槸鐢" "涓殑閮ㄥ垎锛岄櫎浜%寮曞鐨勬牸寮忓瓧绗︿箣澶栵紝鎵鏈夌殑鍏朵粬瀛楃蹇呴』鍦ㄨ緭鍏ユ暟鎹椂鍘熸牱鍘熶綅缃緭鍏ャ備緥濡:scaf("...
绛旓細char a[100][30]={"xxx","abgfds","xcbsd","gksdj","65421","6564","215645"};a[0]="xxx";a[1]="abgfds";a[2]="xcbsd";...鐢ㄤ簩缁存暟缁勫偍瀛 100浠h〃鏈澶氬偍瀛100琛瀛楃涓 30浠h〃姣忚鏈澶氬偍瀛30涓瓧绗
绛旓細鏁扮粍鍒濆鍖栬〃鍐欐硶锛歿鍏冪礌鍒濆煎垪琛▆锛屽叾涓殑鍏冪礌鍒濆煎垪琛浠ラ楀彿琛ㄨ揪寮忓垎闅旓紝濡傦細int a[]={1, 2, 3};double b[]={0.1, 0.2, 0.4};char c[]={'a', 'b', 'c'};
绛旓細鍏堝皢鎵鏈夌殑璇昏繘鏉ュ瓨鍦ㄤ竴涓瀛楃涓涓紝鐒跺悗鐢ㄥ瓧绗﹀垎鍓鍑芥暟strtok()//鍏蜂綋鍙弬瑙丄PI 渚嬪:char str[] = "now # is the time for all # good men to come to the # aid of their country";char delims[] = "#";char *result = NULL;result = strtok( str, delims );while( result != ...