简体   繁体   中英

To read a line from STDIN to extract the numeric tokens only using C

Problem Statement:

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)

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)

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

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

Test Cases:

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

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

See comments in the code...

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

Output:

     45  123     789

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM