Infra_HDC icon

arrays.c

Infra_HDC | PRO | 05/02/19 12:44:15 AM UTC | 0 ⭐ | 754 👁️ | Never ⏰ | []
C |

2.42 KB

|

None

|

0 👍

/

0 👎

#include <stdio.h>
 
#define MAX_N_STRING 100
#define MAX_STRLEN 100
#define STR_FOR_INPUT "%s:100"
#define END_MARKER "END"
#define MY_OWN_STRINGS_H 1
 
#if MY_OWN_STRINGS_H
int strcmp(const char *s1, const char *s2) {
        int i=0;
        while ((s1[i]!='\0') && (s2[i]!='\0') && (s1[i]==s2[i]) && (i<MAX_STRLEN)) {
                i++;
        }
        if ((s1[i]=='\0') && (s2[i]=='\0') && (i<=MAX_STRLEN)) {
                return 0;
        } else {
                return 1;
        }
}
size_t strlen(const char *s) {
        int i=0;
        while ((s[i]!='\0') && (i<(MAX_N_STRING*MAX_STRLEN))) i++;
        return i;
}
char *strcpy(char *dest, const char *src) {
        size_t i;
        for (i=0; (i<MAX_STRLEN) && (src[i]!='\0'); i++) {
                dest[i]=src[i];
        }
        dest[i]='\0';
        return dest;
}
char *strcat(char *dest, const char *src) {
        size_t i=0, j=strlen(src), k=strlen(dest);
        while (i<j) {
                dest[k+i]=src[i]; i++;
        }
        dest[k+i]='\0';
        return dest;
}
#else
#include <string.h>
#endif
 
void main() {
        printf("Hello, World!\nUse single-bit encoding, please!\n");
        printf("Enter strings content. Maximum strings length is %d characters.\n",MAX_STRLEN);
        printf("Input will stopped after %d strings input or the \"%s\" phrase instead of string content.\n",MAX_N_STRING,END_MARKER);
        int i=0; char s[MAX_N_STRING][MAX_STRLEN];
        do {
                printf("s[%d]: ",i); scanf(STR_FOR_INPUT,s[i]);
                if (!strcmp(s[i],END_MARKER)) break;
                i++;
        } while (i<MAX_N_STRING);
        printf("Strings was entered: %d, go to data processing...\n",i);
        for (int j=0; j<i; j++) {
                for (int k=j+1; k<i; k++) {
                        if (strlen(s[k])>strlen(s[j])) {
                                char t[MAX_STRLEN];
                                strcpy(t,s[k]);
                                strcpy(s[k],s[j]);
                                strcpy(s[j],t);
                        }
                }
        }
        printf("Strings array after sorting:\n");
        for (int j=0; j<i; j++) {
                printf("s[%d] = %s\n",j,s[j]);
        }
        char u[MAX_N_STRING*MAX_STRLEN]="";
        for (int j=0; j<i; j++) {
                strcat(u,s[j]);
        }
        printf("The result: %s\n",u);
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎