读书人

c语言指针处理字符串有关问题

发布时间: 2013-07-08 14:13:00 作者: rapoo

c语言,指针处理字符串问题,求助。。。。。。。。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
//将字符串中连续的数字输出为一个数字,并统计数字个数
//比如ser23lkm6345klmo4.w4e'r >>>>a[0]=23,a[1]=6345,a[2]=4,..........

void main()
{
char word[100],*p,a[100][10],*pa;
int i = 0,t;
gets(word);
//把字符串中的非数字字符替换成'\0'
while(word[i] != '\0')
{
if (word[i]<'0' || word[i]>'9')
word[i++] = '\0';
else i++;
}
p = word;

t=i=0;pa = a[0];
while(1)
{//将连续数字输出
if (*p != '\0' && *(p-1)=='\0')
strcpy(a[i++],p);//这里是不是有问题?调试的时候没有传递值到数组
p++;t++;
//检测最后一个'\0'用于终止循环
if (t == strlen(word))
break;
}

printf("%d",i);
for(;i>=0;i--)
puts(a[i]);
}
[解决办法]
[code=c]

// ArrayT.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <malloc.h>
//将字符串中连续的数字输出为一个数字,并统计数字个数
//比如ser23lkm6345klmo4.w4e'r >>>>a[0]=23,a[1]=6345,a[2]=4,..........

void main()
{
char word[100],*p,a[100][10],*pa;
char tmp[100];//临时变量,用于保存没有把word数组中的非字符串替换为'\0'前字符串

int i = 0,t;

int flag = 0;//标识符号,用于判断是否进入下组数字

gets(word);
//把字符串中的非数字字符替换成'\0'
strcpy(tmp,word);
while(word[i] != '\0')
{
if (word[i]<'0'
[解决办法]
word[i]>'9')
word[i++] = '\0';
else i++;
}
p = word;

t=i=0;pa = a[0];
while(1)
{//将连续数字输出
if (flag == 0 && *p != '\0' && *(p-1)=='\0')
{
strcpy(a[i++],p);//这里是不是有问题?调试的时候没有传递值到数组
flag =1;
}

p++;t++;

if ( *p == '\0')
{
flag = 0;
}
//检测最后一个'\0'用于终止循环
if (t == strlen(tmp))
{
a[i][0] = '\0'; //添加结束符
break;

}

}

printf("%d",i);
for(;i>=0;i--)
puts(a[i]);
}

读书人网 >C语言

热点推荐