fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAXL 102 // Max. length
  5. #define MAXN 1002 // Max. number of strings
  6.  
  7. char s[MAXL];
  8. char *ptr[MAXN];
  9. int main()
  10. {
  11. int i, j, n, l, iter=0;
  12.  
  13. // input
  14. printf("Please input the number of strings: ");
  15. scanf("%d", &n); scanf("\n");
  16. for (i = 1; i <= n; i++)
  17. {
  18. gets(s);
  19. l = strlen(s);
  20. ptr[i] = (char *)malloc( sizeof(char)*(l+1) );
  21. strcpy(ptr[i], s);
  22. }
  23.  
  24. // query
  25. for (i = n; i >= 1; i--)
  26. printf("%dth string: %s\n", i, ptr[i]);
  27. return 0;
  28. }
Success #stdin #stdout 0s 2388KB
stdin
4
123
45678
abcdefg
hi
stdout
Please input the number of strings: 4th string: hi
3th string: abcdefg
2th string: 45678
1th string: 123