Having a segmentation error related to arrays/strings

So im trying to check whether two strings are permutations of each other...im doing it by bubble sorting them first then checking if each element matches
im getting a segmentation error but im not able to understand why
http://prntscr.com/garblg
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;
void swape(char a[],int x,int y){
char z;
z = a[x];
a[x]=a[y];
a[y]=z;
}
int len(char a[100]){
                    int counte=0;
                    for(int i=0;a[i]!='\0';i++){
                    counte++;
                    }
                    return counte;
}
void sorte(char c[100]){
    int n = len(c);
 for(int i=n-2;i>=0;i++){
        for(int j=0;j<i;j++){
            if(c[j]>c[j+1]){
                swape(c,j,j+1);
            }
        }

}
}
bool check(char a[],char b[]){
   int n = len(a);
    int k=len(b);
    if(n!=k){
        return false;
    }
sorte(a);
sorte(b);

for(int i=0;a[i]!='\0';i++){
    if(a[i]!=b[i]){
        return false;
    }
}
return true;
}
int main(){
char a[100],b[100];
cin.getline(a,100);
cin.getline(b,100);
check(a,b);
if(check(a,b)){
    cout<<"yep";
}
else{
    cout<<"nope";
}

}

Last edited on
I couldn't find the error in your code so i have rewritten it. Here it is.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstring>
using namespace std;
void sorte(char v[100])
{
    int n=strlen(v);
    for(int i=0; i<=n; i++)
        for(int j=0; j<=n; j++)
            if(v[i]>v[j])
                swap(v[i],v[j]);
}
int main()
{
    char a[100],b[100];
    cin.getline(a,100);
    cin.getline(b,100);
    sorte(a);
    sorte(b);
    if(strcmp(a,b)==0)
        cout<<"yep";
    else
        cout<<"nope";
}

also use the [ code ] [ /code ]
tags next time.
*note*
Don't make your own swap functions, just use swap, to get the lenght use strlen() and to check use strcmp() which will return 0 if they are equal.
Last edited on
All that is fine but as im new to c++ I want to build my own functions initially to get more practice.
Other than that from the image i uploaded it seems that the error has something to do with the memory allocation between the sorte and the check function.
Last edited on
The obvious problem is in the sorte() function.

Line 18
 
    for (int i = n-2; i>=0; i++){
should read
 
    for (int i = n-1; i>=0; i--)


Also in main(), the call to check(a,b); at line 47 is not needed, delete that line.
@Chervil
Thanks it worked but i still have a doubt about i=n-1 instead of n-2;
suppose the array length is n then a[n-1]='\0' while a[n-2]= last character
so while comparing shouldnt i start with n-2 rather than n-1 but doing that doesnt give me the correct answer.
but i still have a doubt about i=n-1 instead of n-2;

I think you are mixed up between the length of the array and the length of the string.

In the original code, the size of the array is always 100. What does change is the length of the text input by the user. Suppose the user types "cat" and presses enter.
The array a will contain
 
{ 'c', 'a', 't', '\0',  ? ? ? etc. }

The function len() will return the value 3.
a[n] is a[3] which is the null terminator.
a[n-1] is a[2] which is the letter 't', the last character of the string.

However, the length of the used part of the array is 4, that's not the same as the string length.
1
2
3
    char test[] = "horse";
    cout << "sizeof(test) = " << sizeof(test) << '\n';
    cout << "len(test)    = " << len(test)    << '\n';

Output:
sizeof(test) = 6
len(test)    = 5


I hope that helps.
Last edited on
thanks man! That really cleared things up for me
Topic archived. No new replies allowed.