Im actually trying to make some program who can check if two strings are permutation from each other. I explain :
If I consider :
Eagle
and
Hdjoh
(I used these two examples in a previous question).
I get a permutation, and the permutation parameter is 3. Why ? Because in the alphabet : E + 3 = H, a + 3 = d etc..
I used unsigned char because if I get a z in one of my strings, I want that (for example) z + 3 = c.
What I started to do :
#include <stdio.h>
#define N 20
int my_strlen(unsigned char *string){
int length;
for (length = 0; *string != '\0'; string++){
length++;
}
return(length);
}
int main()
{
unsigned char string1[N], string2[N];
int test=0, i=0, length1, length2;
scanf("%s", string1);
scanf("%s", string2);
length1=my_strlen(string1);
length2=my_strlen(string2);
if(length1==length2){
for(i=0; i<length1; i++){
if(string1[i]==string2[i]){
test=1;
}
else{
test=0;
}
}
printf("Test = %d", test);
}
else{
printf("Error");
}
return 0;
}
I just started to think about it.. So for the moment I just try to compare the two strings letter by letter.
The problem here : If i try to compare Hello and hello, or Hello and Helqo I get Test = 1.
So someone can tell me whats wrong here ?
Thanks a lot.
EDIT 1 :
#include <stdio.h>
#define N 20
int my_strlen(unsigned char *string){
int length;
for (length = 0; *string != '\0'; string++){
length++;
}
return(length);
}
int main()
{
unsigned char string1[N], string2[N];
int test=0, i=0, length1, length2;
scanf("%s", string1);
scanf("%s", string2);
length1=my_strlen(string1);
length2=my_strlen(string2);
if(length1==length2){
for(i=0; i<length1; i++){
if(string1[i]==string2[i]){
test=1;
}
else{
test=0;
break;
}
}
printf("Test = %d", test);
}
else{
printf("Error");
}
return 0;
}
Now it's correct. I will continue.
EDIT 2 - 6.7.14 :
I am actually working and the "second part" of the program. I am looking for the d and I verify if its a permutation or not. No so easy so I need some advices, do I have to write an other function to do this ? Or just working on this part of my code :
if(length1==length2){
for(i=0; i<length1; i++){
if(string1[i]==string2[i]){
test=1;
}
else{
test=0;
break;
}
}
printf("Test = %d", test);
}
else{
printf("Error");
}
return 0;
}
I wrote it like this for the moment :
if(length1==length2){
for(i=0; i<length1; i++){
for(d=0; d<255; d++){
if(string1[i]==string2[i] + d){
permutation=1;
}
else{
permutation=0;
break;
}
}
}
printf("\nPermutation = %d \nd = %d", permutation, d);
}
else{
printf("Not a permutation");
}
return 0;
}
(I know that it doesn't work but I just tried..).
Thanks by advance for the help.