Comparing 2 Strings in C

Nov 10, 2012 at 5:22pm
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#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;
                                                             }
Nov 10, 2012 at 5:36pm
Found The answer should have put s2[j] and s1[i] are diferrent from \0.
Nov 10, 2012 at 6:27pm
More importantly, you need to fix your indentation.
If this is for school or college, you can't hand it in like that.
Topic archived. No new replies allowed.