以空格截断数据

/*
* 程序功能:以空格 tab 截断的单词,将大写转换为小写
* 作者版本日期:20151110 zhouhaib
* 源代码:ntpconf.c
* 代码存储位置 :
* 整体思路 :
*/

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAX_LINE_SIZE 1024

int split_string(char *str,char **array)
{
int cnt=(int) strlen(str);
char *p = str;
while((*p==' ')||(*p=='\t'))//遇到tab和空格处理
p++;
if(p>=str+cnt) return 0;//不输入
int ii=0;
char *b=(char *)malloc(100);
memset(b,0,100);
int j=0;
while(1)
{
if(p >=str+cnt) break;
if((*p =='\r')||(*p =='\n')) break; //换行
while((*p !=' ')&&(*p !='\t')&&(*p !='\r')&&(*p !='\n'))
{
b[j++] = tolower(*p);//把字符转换成小写字母,非字母字符不做出处理
p++;
}
array[ii++] =b;
b=(char *)malloc(100);
memset(b,0,100);
j=0;
if((*p =='\r')||(*p =='\n')) break;
while((*p==' ')||(*p =='\t'))
{
p++;
if(p >=str+cnt) break;
}
}
return ii;
}

int main(int argc,char* argv[])
{
char buff[MAX_LINE_SIZE]={0};
char *array[10];
memset(array,0,sizeof(char*)*10);
int num,i;

printf("the word split by ' ' \n");

while(fgets(buff,MAX_LINE_SIZE,stdin))
{
printf("the word split by ' ' \n");

num =split_string(buff,array);

printf("num=%d\n",num);
for(i=0;i<num;i++)
{
printf("array[i]=%s\n",array[i]);
}

printf("input the word\n");
}
}

posted @ 2015-11-10 15:01  周人假的  阅读(347)  评论(0编辑  收藏  举报