Description
For this question, your program is required to process an input string containing only ASCII characters between ‘0’ and ‘9’, or between ‘a’ and ‘z’ (including ‘0’, ‘9’, ‘a’, ‘z’).
Your program should reorder and split all input string characters into multiple segments, and output all segments as one concatenated string. The following requirements should also be met,
1. Characters in each segment should be in strictly increasing order. For ordering, ‘9’ is larger than ‘0’, ‘a’ is larger than ‘9’, and ‘z’ is larger than ‘a’ (basically following ASCII character order).
2. Characters in the second segment must be the same as or a subset of the first segment; and every following segment must be the same as or a subset of its previous segment.
Your program should output string “<invalid input string>” when the input contains any invalid characters (i.e., outside the '0'-'9' and 'a'-'z' range).
Input
Input consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.
Output
For this question, your program is required to process an input string containing only ASCII characters between ‘0’ and ‘9’, or between ‘a’ and ‘z’ (including ‘0’, ‘9’, ‘a’, ‘z’).
Your program should reorder and split all input string characters into multiple segments, and output all segments as one concatenated string. The following requirements should also be met,
1. Characters in each segment should be in strictly increasing order. For ordering, ‘9’ is larger than ‘0’, ‘a’ is larger than ‘9’, and ‘z’ is larger than ‘a’ (basically following ASCII character order).
2. Characters in the second segment must be the same as or a subset of the first segment; and every following segment must be the same as or a subset of its previous segment.
Your program should output string “<invalid input string>” when the input contains any invalid characters (i.e., outside the '0'-'9' and 'a'-'z' range).
Input
Input consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.
Output
For each case, print exactly one line with the reordered string based on the criteria above.
#include <stdio.h>
#include <string.h>
#define MAXWORDS 200
void character_sort(char s[], int n);
void final_process(char s[], int n);
int main()
{
char str[MAXWORDS];
char c;
int i, j, num;
memset(str,'@',sizeof(str));
//memset(str2,0,sizeof(str1[1]));
i = 0;
j = 0;
num = 0;
printf("INPUT THE STRING YOU WANT!\n");
while((c = getchar()) != '\n')
{
//str1[0][i] = c;
if ((48 <= c && c <= 57) || (97 <= c && c <= 122))
{
str[i] = c;
i++;
num++;
}
else
{
printf("<invalid input string>\n");
return -1;
}
}
character_sort(str,num);
for(i = 0; i < num; i++)
{
printf("%c", str[i]);
}
printf("\n");
final_process(str,num);
i = 0;
while((str[i] != '@'))
{
printf("%c", str[i]);
i++;
}
return 0;
}
void character_sort(char A[], int N)
{
char tmp;
int i,j;
for (i = 0; i < N - 1; i++)
{
for (j = i + 1; j < N; j++)
{
if (A[j] < A[i])
{
tmp = A[j];
A[j] = A[i];
A[i] = tmp;
}
}
}
}
void final_process(char str[], int num)
{
char str1[2][MAXWORDS];
char str2[MAXWORDS];
int i, j, k, tmp, tmp1;
memset(str1[0],'@',sizeof(str1[0]));
memset(str1[1],'2',sizeof(str1[1]));
memset(str2,'@',sizeof(str2));
i = 0;
j = 0;
k = 0;
for (i = 0; i < num; i++)
{
str1[0][i] = str[i];
}
for (i = 0; i < num; i++)
{
if (str1[1][i] == '2')
{
str2[k] = str1[0][i];
tmp = str1[0][i];
for (j = 0; j < num; j++)
{
if (str1[1][j] == '2')
{
if (str1[0][j] > tmp)
{
str2[++k] = str1[0][j];
str1[1][j] = 1;
tmp1 = str1[0][j];
tmp = tmp1;
}
}
}
k++;
}
}
for (i = 0; i < MAXWORDS; i++)
{
str[i] = str2[i];
}
}