从C中的字符串中提取数字

时间:2017-05-20 15:11:40

标签: c string

请告诉我我写的程序有什么问题? 我试图创建一个新字符串,其中包含用户输入的字符串中的数字。

  

例如:"输入一个字符串:helloeveryone58985hohoh kgkfgk878788

     

回答:58985878788

     

如果找不到数字,那么答案应该是:"字符串没有变化。"

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

#define MK 20
#define ML 81

void changeStr(char str[],char New[]){
    int i,j=0,n;

    for(i=0;i<)
}

int main(){
    char str[ML],New[ML]={0};
    printf("Enter string: \n");
    gets(str);
    changeStr(str,New);
    printf("Changed string:\n");
    printf("%s",New);
    if(New[0] == '\0'){
        printf("No changes in string.\n");
    }
    return 0;
}

2 个答案:

答案 0 :(得分:1)

这应该有效:

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

#define ML 81

char    *changeStr(char *str)
{
    char *new = NULL;;
    int i = 0;
    int length = 0;

    /* calulating the size to allocate with malloc for new */
    while (str[i])
    {
        if (str[i] >= 48 && str[i] <= 57)
            length++;
        i++;
    }

    /* if no numbers found, return new which is NULL */
    if (length == 0)
        return new;
    new = malloc(length * sizeof(char));
    i = 0;
    length = 0;

    /* filling new with numbers */
    while (str[i])
    {
        if (str[i] >= 48 && str[i] <= 57)
        {
            new[length] = str[i];
            length++;
        }
        i++;
    }
    new[length] = 0;
    return new;
}

/* I kept the functions you are using in the main, i would not
   use gets, but it's maybe easier for you to keep it */

int main()
{
    char str[ML]={0};
    char *New;

    printf("Enter string: \n");
    gets(str);
    New = changeStr(str);
    if(!New){
        printf("No changes in string.\n");
    }
    else
    {
        printf("Changed string:\n");
        printf("%s",New);
    }
    return 0;
}

答案 1 :(得分:0)

是的,你想要的是什么?

IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling
相关问题