Search code examples
cc99c89

C programming Strings printing


I have a question on this string .

for example:

char ex1[20]="Hello hi";
int choose;
scanf("%d",&choose);

What should I do to make it print "hi" when user enters 1 and "hello" would be printed if he enters 0?

Thank you for your help.


Solution

  • I guess you want this.

    #include<stdio.h>
    #include<string.h>
    void split(char*str, char** arr) {
        char* str2 = strstr(str, " ");
        *str2 = '\0';
        str2++;
        arr[0] = str;
        arr[1] = str2;
    }
    int main(void) {
        char ex1[20] = "Hello hi";
    
        char*arr[2];
        split(ex1, arr);
    
        int choose;
        scanf("%d", &choose);
        switch (choose) {
        case 0:
            puts(arr[0]);
            break;
        case 1:
            puts(arr[1]);
        }
    
        return 0;
    }
    

    The results are as follows

    enter image description here

    enter image description here