C語言數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)字符串分割的實(shí)例
更新時(shí)間:2017年10月11日 08:56:03 作者:愛思考的小鳥
這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)字符串分割的實(shí)例的相關(guān)資料,希望通過本文能幫助到大家實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下
C語言數(shù)據(jù)結(jié)構(gòu)實(shí)現(xiàn)字符串分割的實(shí)例
以下為“字符串分割”的簡單示例:
1. 用c語言實(shí)現(xiàn)的版本
#include<stdio.h> /* 根據(jù)空格分隔字符串 */ int partition(char *src, char *par, int pos) { int i,j; i = pos; //取到第一個(gè)非空格字符 while(src[i] == ' ') { ++i; } if(src[i] != '\0') { j = 0; while((src[i] != '\0') && (src[i] != ' ')) { par[j] = src[i]; ++i; ++j; } par[j]='\0'; return i; } else { return -1; } } void main() { char string[50]; char partition_string[20]; int position; int k; printf("Please input strng(length<=50): "); gets(string); position=0; printf("\nPartition result: \n"); k=0; while((position = partition(string,partition_string,position)) != -1) { ++k; printf("Partition %d : %s\n", k, partition_string); } }
運(yùn)行結(jié)果如下所示:
如有疑問請(qǐng)留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!