-5

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

Mohamed Sahir
  • 2,482
  • 8
  • 40
  • 71

1 Answers1

1

As mentioned by PaulMcKenzie, it seems that you apply concepts of other languages directly to the c programming language, mostly by guessing. If you do not have a book to start with, use at least google to find some tutorials. For example, I entered "c strings introduction" and got - among others - this link, which explains a lot of the issues you are currently having.

The following does not answer your question or even solve your homework, but maybe it gives you enough hints to start.

A "string" in c is an array of characters, terminated by a 0 (often written as '\0'.

String literals are enclosed in double quotes, i.e. "hello" (not 'hello').

Single characters of a string can be accessed by an index, e.g. char c = ch[0] of char c = ch[5] or int i=3; char c = ch[i].

A loop that iterates through the characters of a string and looks for an A could look as follows:

int i=0;
int found = 0;
while (!found && i < 80 && ch[i] != '\0') {
   if (ch[i] == 'A')
     found = 1;

   i++;
} 

This is not meant as an answer; It is just that I am a beginner, too (though in other fields than C); and beginners should help beginners :-) Have fun learning C, use google and good books, and be aware that it will take some time.

Stephan Lechner
  • 34,891
  • 4
  • 35
  • 58
  • thanks stephen suddenly i thought to learn c because of performance wise c and c++ occupies in first read by articles therefore apply the other language sand c instructions comparing and learning, while string manipulation somewhat struggling in c, so posted as question to get different thought of a progrmmersi am new to c ,and know string contains group of characters , so the collection will be split out by characters and will check and process it by using itertaions . I will read more articles and learn c string manipulations more to improve logical wise – Mohamed Sahir Mar 01 '17 at 17:50