C语言数据结构实现字符串分割的实例
以下为“字符串分割”的简单示例:
1. 用c语言实现的版本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
#include<stdio.h>
/* 根据空格分隔字符串 */
int partition( char *src, char *par, int pos)
{
int i,j;
i = pos;
//取到第一个非空格字符
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);
}
}
|
运行结果如下所示:
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
原文链接:http://blog.csdn.net/chinawangfei/article/details/52947436