Click here to Skip to main content
15,846,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have to make a program that take name of user and when two specific names like (Alice and Bob) these names given by user than program say hello to them like this:
What is your name?
Alice
Hello! Alice.
Ben
I don't know you Ben
I want to make this program in C language without using string.

What I have tried:

I tried this :(
C++
#include <stdio.h>

int main()
{
  
  char name[30];
  
   printf("Enter your name please!\n");
   scanf("%s", name);
   if (name=="Alice"||name=="Bob")
   {
   printf("Hello %s" , &name);
   }
   else
   {
   printf("I don't know you %s", name);
   }

   return 0;
}
Posted
Updated 26-Mar-23 14:38pm
v3

You will need a loop of some form: C - Loops[^], but you can't compare user input to a string literaL in C - it just doesn't have any idea how to do that.

To compare two null terminated strings in C, you will either have to use library functions such as strcmp: C strcmp() - C Standard Library[^] or write your own character-by-character comparison code.

Do bear in mind that upper and lower case characters are different: 'B' is not the same as 'b' and a simple compare will not match "Bob" and "bob".
 
Share this answer
 
v3
Comments
CPallini 26-Mar-23 12:17pm    
5.
Direct comparison of strings doesn't work that way in C, but it would probably be easy to do that with a self-written function.
C
const char* knownnames[3] = { "Alice", "Bob", NULL };

// ...

if (chk_known_names(name, knownnames)) {
	printf("Hello %s", &name);
} 
else { // ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900