Advertisement
Infra_HDC

arrays.c

May 1st, 2019
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. #define MAX_N_STRING 100
  4. #define MAX_STRLEN 100
  5. #define STR_FOR_INPUT "%s:100"
  6. #define END_MARKER "END"
  7. #define MY_OWN_STRINGS_H 1
  8.  
  9. #if MY_OWN_STRINGS_H
  10. int strcmp(const char *s1, const char *s2) {
  11.         int i=0;
  12.         while ((s1[i]!='\0') && (s2[i]!='\0') && (s1[i]==s2[i]) && (i<MAX_STRLEN)) {
  13.                 i++;
  14.         }
  15.         if ((s1[i]=='\0') && (s2[i]=='\0') && (i<=MAX_STRLEN)) {
  16.                 return 0;
  17.         } else {
  18.                 return 1;
  19.         }
  20. }
  21. size_t strlen(const char *s) {
  22.         int i=0;
  23.         while ((s[i]!='\0') && (i<(MAX_N_STRING*MAX_STRLEN))) i++;
  24.         return i;
  25. }
  26. char *strcpy(char *dest, const char *src) {
  27.         size_t i;
  28.         for (i=0; (i<MAX_STRLEN) && (src[i]!='\0'); i++) {
  29.                 dest[i]=src[i];
  30.         }
  31.         dest[i]='\0';
  32.         return dest;
  33. }
  34. char *strcat(char *dest, const char *src) {
  35.         size_t i=0, j=strlen(src), k=strlen(dest);
  36.         while (i<j) {
  37.                 dest[k+i]=src[i]; i++;
  38.         }
  39.         dest[k+i]='\0';
  40.         return dest;
  41. }
  42. #else
  43. #include <string.h>
  44. #endif
  45.  
  46. void main() {
  47.         printf("Hello, World!\nUse single-bit encoding, please!\n");
  48.         printf("Enter strings content. Maximum strings length is %d characters.\n",MAX_STRLEN);
  49.         printf("Input will stopped after %d strings input or the \"%s\" phrase instead of string content.\n",MAX_N_STRING,END_MARKER);
  50.         int i=0; char s[MAX_N_STRING][MAX_STRLEN];
  51.         do {
  52.                 printf("s[%d]: ",i); scanf(STR_FOR_INPUT,s[i]);
  53.                 if (!strcmp(s[i],END_MARKER)) break;
  54.                 i++;
  55.         } while (i<MAX_N_STRING);
  56.         printf("Strings was entered: %d, go to data processing...\n",i);
  57.         for (int j=0; j<i; j++) {
  58.                 for (int k=j+1; k<i; k++) {
  59.                         if (strlen(s[k])>strlen(s[j])) {
  60.                                 char t[MAX_STRLEN];
  61.                                 strcpy(t,s[k]);
  62.                                 strcpy(s[k],s[j]);
  63.                                 strcpy(s[j],t);
  64.                         }
  65.                 }
  66.         }
  67.         printf("Strings array after sorting:\n");
  68.         for (int j=0; j<i; j++) {
  69.                 printf("s[%d] = %s\n",j,s[j]);
  70.         }
  71.         char u[MAX_N_STRING*MAX_STRLEN]="";
  72.         for (int j=0; j<i; j++) {
  73.                 strcat(u,s[j]);
  74.         }
  75.         printf("The result: %s\n",u);
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement