鱼C论坛

 找回密码
 立即注册
查看: 2566|回复: 4

输入一个字符串,内有数字和非数字字符 如a123x4 将连续的数字作为一个元素放入数组

[复制链接]
发表于 2017-4-21 12:11:46 | 显示全部楼层 |阅读模式
10鱼币
如果字符串连续输入两个字符的话 为什么b = 0不会被放到整形数组中?

QQ截图20170421120943.jpg
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-21 12:18:50 | 显示全部楼层
把代码贴上来
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-4-21 14:10:15 | 显示全部楼层

#include <stdio.h>
void main()
{
void sum(char *p);
char s[50],*p=s;
gets(s);
sum(p);
}

void sum(char*p)
{
int a[10],i,j=0,b=0;
for(i=0;*(p+i)!='\0';i++)
{
if(*(p+i)>='0'&&*(p+i)<='9')
{
b=*(p+i)-48+b*10;
if(*(p+i+1)<'0'||*(p+i+1)>'9')
{
a[j]=b;
j++;
b=0;
}
}
}
for(i=0;i<j;i++)
printf("%d ",a);
printf("\n");
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-21 16:43:39 | 显示全部楼层
不明白你想问什么
无标题.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-4-21 17:30:19 | 显示全部楼层
我写了一个类似的
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>

  4. char **get_string(char *str)
  5. {
  6.         char buf[20];
  7.         int index = 0;
  8.         char **ret = NULL;
  9.         int ret_point_count = 0;

  10.         while(1)
  11.         {
  12.                 while(*str <= '0' || *str >= '9')
  13.                 {
  14.                         if(*str == '\0') goto e;
  15.                         str++;
  16.                 }

  17.                 index = 0;
  18.                 while(*str >= '0' && *str <= '9') buf[index++] = *str++;
  19.                 buf[index++] = '\0';

  20.                 ret_point_count++;
  21.                 ret = realloc(ret, sizeof(char **) * ret_point_count);
  22.                 ret[ret_point_count - 1] = malloc(strlen(buf) + 1);
  23.                 strcpy(ret[ret_point_count - 1], buf);
  24.         }

  25. e:
  26.         ret_point_count++;
  27.         ret = realloc(ret, sizeof(char **) * ret_point_count);
  28.         ret[ret_point_count - 1] = NULL;

  29.         return ret;
  30. }

  31. int main(void)
  32. {
  33.         char s[50];
  34.         char **str_arr;
  35.        
  36.         fgets(s, 50, stdin);
  37.         str_arr = get_string(s);

  38.         for(int i = 0; str_arr[i] != NULL; i++)
  39.         {
  40.                 puts(str_arr[i]);
  41.         }

  42.         return 0;
  43. }
复制代码

  1. 123qwer4567sdfg567rtyufgh6789tyu5678
  2. 123
  3. 4567
  4. 567
  5. 6789
  6. 5678
  7. 请按任意键继续. . .
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 07:08

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表