要从STDIN读取一行以仅使用C提取数字标记 [英] To read a line from STDIN to extract the numeric tokens only using C

查看:68
本文介绍了要从STDIN读取一行以仅使用C提取数字标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题陈述:

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

Need to process an input string, accepted from STDIN. and find only all the numeric tokens that are present in the string. Consider tokens to be sequence of printable characters separated by space(s). (In numeric tokens all characters are digits)

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

You need to build a new string which is of the form numeric_token1 numeric_token2 print this in ascending order. (A single space is the separator) (If no numeric tokens are found you need to print NONE FOUND)

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

Input : We need to read a line from STDIN to extract the numeric tokens only

输出:由数字1到数字2升序组成的字符串.还是找不到

Output : The string composed of number1 number2 in ascending order . Or NONE FOUND

测试用例:

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

我尝试使用静态方式通过以下方式标记和升序排列它们,但还不够幸运.我是这些令牌的新手,我们将不胜感激.

I tried the below way, using a static way, to tokenise and rearrange them in ascending order but was not lucky enough. I am new to these tokens any help is greatly appreciated.

#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; 
}

推荐答案

使用 fgets() 读取字符串 strtok() 将其拆分为令牌, isdigit() 检查令牌是否为数字,>> atoi() 将字符串转换为数字,然后 qsort() 对字符串进行排序.

Use fgets() to read the string, strtok() to split it into tokens, isdigit() to check whether a token is a number or not, atoi() to convert a string to a number, and qsort() to sort the string.

#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;
}

这篇关于要从STDIN读取一行以仅使用C提取数字标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆