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 consists of multiple cases, one case per line. Each case is one string consisting of ASCII characters.
For each case, print exactly one line with the reordered string based on the criteria above.
- 01.#include<stdio.h>
- 02.#include<stdlib.h>
- 03.#include<string.h>
- 04.char temp[10][100];
- 05.
- 06.void process(char *String)
- 07.{
- 08. int temp1[256] = {0};
- 09.
- 10. int index = 0;
- 11.
- 12. if(String==NULL)
- 13. return ;
- 14.
- 15. int len = strlen(String);
- 16. int i;
- 17. for(i = 0; i < len; i++)
- 18. {
- 19. if((String[i] >='0' && String[i] <='9') || (String[i] >= 'a' && String[i] <='z') )
- 20. {
- 21. index = String[i] - '0';
- 22. temp1[index]++;
- 23. }
- 24. else
- 25. {
- 26. printf("<invalid input string>");
- 27. }
- 28. }
- 29.
- 30. index = 0;
- 31.
- 32. while(1)
- 33. {
- 34. for(i = 0; i < 256; i++)
- 35. {
- 36.
- 37. if(temp1[i] !=0)
- 38. {
- 39. String[index] = i + '0';
- 40. index++;
- 41. temp1[i]--;
- 42. }
- 43.
- 44. }
- 45.
- 46. if(index == len)
- 47. {
- 48. printf("%s\n", String);
- 49. return;
- 50. }
- 51. }
- 52.}
- 53.
- 54.int main()
- 55.{
- 56. int i = 0;
- 57. while(gets(temp[i++]) != NULL);
- 58. i--;
- 59. int j;
- 60. for(j = 0; j<i; j++)
- 61. {
- 62. process(temp[j]);
- 63. }
- 64. return 0;
- 65.}
- 66.