comparing strings

two strings of unknown length. how can we compare them? i am trying the following method. it is giving segmentation fault. I guess the problem is in line 5 and 10 but i cant really figure out what to write there instead of m and n.......
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
        int m;
        int n;
        char string1[m];
        char string2[n];
        if(string1[m]==string2[n])
        {
                return 0;
                cout << "strings are equal" << endl;
        }
        if(string1[m]!=string2[n])
        {
                int a=0;
                while(string1[a]==string2[a])
                {
                        a++;
                }
                if(string1[a]>string2[a])
                {
                        return 1;
                }
                if(string1[a]<string2[a])
                {
                        return -1;
                }

         }


why do not you use


if( strcmp(string1,string2))
because i dont want to use string library
Get the two character arrays. Look at first element in each.

a) if they're different, strings are not the same. Stop
b) if one is zero and the other is not, strings are not the same. Stop
c) if both are zero, the strings are the same. Stop.
b) if both same and not zero, strings could be the same. Move on to next element and repeat.

1
2
3
4
5
6
7
8
9
10
11
12
13
 if(string1[m]==string2[n])
        {
                return 0;
                cout << "strings are equal" << endl;
        }


 if(string1[m]==string2[n])
        {
          
                cout << "strings are equal" << endl;
                return 0;
        }


you must write return 0 after output!
and about your error if m>n or n>m then you will get the segmentation error because.... the point is when two strings are having different length how they can be equal?
The logic of your comparison look almost right. But if the strings are equal, you have to stop at the null term, so your while loop needs modifying a bit. Also, you need to return 0 if neither of the comparisons hold.

You initial check should be:

1
2
3
4
5
6
7
8
9
        if(string1 == string2)
        {
                cout << "strings are equal" << endl;
                return 0;
        }
        else
        {
                // do actual comparison...
         }


Note that if something isn't "equal", it must be "not equal", so you can use an else! Here I have assumed that you want to see if the addresses of the strings are idential, which would means you comparing a string with itself.

Your example doesn't init the string.

1
2
3
4
5
6
7
        const int m = 10; // in ANSI C++, array inti required const size
        const int n = 15;
        char string1[m] = "";
        char string2[n] = "";

        strcpy(string1, "hello");
        strcpy(string2, "world");


or, for testing purposes

1
2
        const char string1[] = "hello";
        const char string2[] = "world";


Note that the elements of a string declared string[m] range from 0 ... m-1. string[m] is one beyond the end of the memory owned by the string.
Last edited on
None mentioned? The elements index of any array starts with 0 thus the last index is 'the number of elements'-1, thus making

1
2
3
char string1[m];
char string2[n];
if(string1[m]==string2[n]) {...}


always cause a segmentation fault since the two arrays last valid index is m-1 and n-1.

Otherwise dunno if someone mentioned this before, but the following (boundly corrected):

1
2
3
4
5
if(string1[m-1]==string2[n-1])
        {
                return 0;
                cout << "strings are equal" << endl;
        }


will return true if their last letter is the same, which i guess you didnt mean.
Last edited on
Topic archived. No new replies allowed.