I need to compare the user enter values in string two string without using Inbuilt functions or(strcmp) in c. i have used first program by using other languages its not working so i have created the second one as working in c ,is there any better way without while loop:
This question will help the people who have some basic knowledge in c and good understanding knowledge like javascript or any other oops language programmers suddenly interest to learn c programming concepts how c string manipulation gets different from other languages and want to play with string manipulation without using any built-in function in c.
#include<stdio.h>
void main()
{
char ch[80];
printf("Enter Your name:" );
if(scanf("%s",ch) =="hello")
{
printf("hello");
}
else if(scanf("%s",ch) =="saythanku")
{
printf("saythanku");
}
else
{
printf("none");
}
}
Working:
#include<stdio.h>
void main()
{
printf("enter your name");
char ch[80];
scanf("%s",ch);
int a=compare(ch,"hello");
if(a==0)
{
printf("hello");
}
else
{
printf("not hello");
}
}
int compare(char a[], char b[])
{
int c = 0;
while( a[c] == b[c] )
{
if( a[c] == '\0' || b[c] == '\0' )
break;
c++;
}
if( a[c] == '\0' && b[c] == '\0' )
return 0;
else
return -1;
}
simple any otherways to compare