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