从STDIN读取一行仅使用C提取数字标记

时间:2017-09-30 03:07:31

标签: c arrays string loops numbers

问题陈述:

需要处理从STDIN接受的输入字符串。并且只查找字符串中存在的所有数字标记。 将标记视为由空格分隔的可打印字符序列。 (在数字标记中,所有字符均为数字)

您需要构建一个新的字符串,其格式为numeric_token1 numeric_token2以升序打印。 (单个空格是分隔符)(如果找不到数字标记,则需要打印NONE FOUND)

输入:我们需要从STDIN中读取一行以仅提取数字标记

输出:由number1 number2按升序组成的字符串。或者没找到

测试案例:

Input: hello hi 123  789 45 hi
Output: 45 123 789

Input: 20 abc beg 90 67
Output: 20 67 90

Input: hi hello foo bar foo
Output: NONE FOUND

我尝试了以下方式,使用静态方式,按升序进行标记和重新排列,但不够幸运。我是这些令牌的新手,非常感谢任何帮助。

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

char* RemoveCharac(char* input) 
{ 
   char* dest = input;
   char* src = input;

   while(*src) 
   { 
      if (isalpha(*src)) 
         { 
         src++; 
         continue; 
         } 

      *dest++ = *src++; 
   } 

   *dest = '\0'; 
   return input; 
} 

int main(void) 
{ 
   char inText[] = "hello hi 123  789 45 hi"; 
   char *pch; 
   char* strArray[1024];
   char* ResText = RemoveCharac(inText);

   int i = 0,j; 
   printf("The result is %s\n", ResText); 
   pch = strtok (ResText," ,.-"); 
   while (pch != NULL) 
   { 
      strArray[i] = malloc(strlen(pch) + 1); 
      strcpy(strArray[i], pch); 
      printf ("%s ",pch); 
      pch = strtok (NULL, " ,.-"); 
      i++; 
   }

   printf ("\n"); 
   for(j=0;j<i;j++) 
   { 
      printf("\t %s",strArray[i]); 
   } 

   return 0; 
}

2 个答案:

答案 0 :(得分:2)

使用fgets()读取字符串,strtok()将其拆分为令牌,isdigit()以检查令牌是否为数字,atoi()转换字符串到一个数字,qsort()对字符串进行排序。

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

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main(void) {
    char input[100];
    int tokens[20], counter = 0;
    fgets(input, sizeof(input), stdin);
    input[strcspn(input, "\n")] = 0;

    char * pch = strtok (input," ");
    while (pch != NULL)
    {
        int isNumber = 1;
        //printf ("%s\n", pch);
        for (size_t i = 0; i < strlen(pch); ++i)
            if (!isdigit(pch[i]))
                isNumber = 0;
        if(isNumber)
            tokens[counter++] = atoi(pch);
        pch = strtok (NULL, " ");
    }
    qsort (tokens, counter, sizeof(int), compare);
    for(int i = 0; i < counter; ++i)
        printf("%d ", tokens[i]);
    if(!counter)
        printf("NONE FOUND");
    printf("\n");
    return 0;
}

答案 1 :(得分:2)

查看代码中的评论......

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

int CompareElements(const void * a, const void * b)
   {
   return(atol(*(char **)a) - atol(*(char **)b));
   }

char* RemoveCharac(char* input)
   {
   char* dest = input;
   char* src = input;

   while(*src)
      {
      if(isalpha(*src))
         {
         src++;
         continue;
         }

      *dest++ = *src++;
      }

   *dest = '\0';
   return input;
   }

int main(void)
   {
   char inText[] = "hello hi 123  789 45 hi";
   char *pch;
   char* strArray[1024];
   char* ResText = RemoveCharac(inText);

   int i = 0,j;
   printf("The result is %s\n", ResText);
   pch = strtok (ResText," ,.-");
   while(pch != NULL)
      {
      strArray[i] = malloc(strlen(pch) + 1);
      strcpy(strArray[i], pch);
      printf ("%s ",pch);
      pch = strtok (NULL, " ,.-");
      i++;
      }
   printf ("\n");

   /* Sort the list HERE... */
   qsort(strArray, i, sizeof(char *), CompareElements);

   for(j=0; j<i; j++)
      {
//    printf("\t %s",strArray[i]);   Oops... It should be [j] not [i]
      printf("\t %s",strArray[j]);
      }
   printf ("\n");

   return 0;

输出:

     45  123     789