Comparing 2 Strings in C

This is a discussion on Comparing 2 Strings in C within the C Programming forums, part of the General Programming Boards category; Hi all. I am trying to create a program which will compare 2 strings.Find the first character which is equal ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    2

    Lightbulb Comparing 2 Strings in C

    Hi all.
    I am trying to create a program which will compare 2 strings.Find the first character which is equal between them and save it on the third string.If it doesnt find any character it will save a -1.
    My code always prints a -1...
    Here is my code,I dont know what I am doing wrong.If anybody has any idea,plz help me.

    Code:
    #include <stdio.h>
    #define MAX 1000
    void any(char s1[],char s2[],char s3[]);
    int main()
    {
        char string1[MAX],string2[MAX],string3[MAX];
        printf("Jepni stringen 1\n");
        scanf("%s",& string1);   //saving string 1
        printf("Jepni stringen 2\n");
        scanf("%s",& string2);  //saving string 2
        any(string1,string2,string3); /*comparing characters from string 2 to string 1 and saving the places where they are equal on third string*/
        printf("%d",string3[0]); //printing the first character of the third string
        return 0;
    }
    void any(char s1[],char s2[],char s3[])
    {
         int i,j,k;
         k=0;
         for(j=0;j!='\0';j++)
         {
                             for(i=0;i!='\0';i++)
                             {
                                                 if(s2[j]==s1[i])
                                                 {
                                                                 s3[k]=i;
                                                                 j++;
                                                                 k++;
                                                                 }
                                                                 }
                                                                 }
                                                                 s3[k]=-1;
                                                                 }

  2. #2
    Registered User
    Join Date
    Nov 2012
    Posts
    2
    Found The answer should have put s2[j] and s1[i] are diferrent from \0.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Comparing strings
    By bungkai in forum C Programming
    Replies: 6
    Last Post: 01-20-2012, 05:06 PM
  2. Comparing two Strings
    By DJORDJE in forum C++ Programming
    Replies: 7
    Last Post: 12-26-2011, 04:00 PM
  3. Comparing Strings
    By RazorBlade in forum C++ Programming
    Replies: 16
    Last Post: 12-13-2007, 12:54 PM
  4. Comparing strings
    By manutd in forum C++ Programming
    Replies: 2
    Last Post: 11-08-2006, 05:49 PM
  5. Comparing 2 Strings
    By Unregistered in forum C++ Programming
    Replies: 4
    Last Post: 01-08-2002, 06:01 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21