-1

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.

lurker
  • 56,987
  • 9
  • 69
  • 103

3 Answers3

2

The simplest solution would be:

if(choose == 1)
    printf("Hello\n");
else if(choose == 0)
    printf("hi\n");
else
    printf("Please enter 1 or 0.\n");

You don't need ex1 here.

user253751
  • 57,427
  • 7
  • 48
  • 90
1

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

Jerry Chou
  • 180
  • 1
  • 11
0

Is this what you mean? I'm not sure exactly what you mean.

char buf[2];
fgets(buf, 2, stdin);
*buf == '1' ? puts("hi") : puts("hello");

This reads the first two characters from stdin, the standard input stream, and then prints "hi" if the first character is 1, or "hello" otherwise.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
lost_in_the_source
  • 10,998
  • 9
  • 46
  • 75