Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › c programming string comparison
New Posts  All Forums:Forum Nav:

c programming string comparison

post #1 of 13
Thread Starter 
the program is supposed get two strings from the input and then compare the strings, character by character to find whether they are same or
not and then it is supposed to find the position of the first different character and print it out. Without using the string library(string.h)

output Sample:
Enter first string: I like C very much.
Enter second string: I like C++ very much.
Strings are not equal.
The first difference occurs in: 8
Do You Want to Continue: y/n



My current program looks like this

#include

int compare_string(char*, char*);

int main()
{
char first[100], second[100], result;

printf("Enter first string\n");
gets(first);

printf("Enter second string\n");
gets(second);

result = compare_string(first, second);

if ( result == 0 )
printf("Both strings are same.\n");
else
printf("Entered strings are not equal.\n");

return 0;
}

int compare_string(char *first, char *second)
{
while(*first==*second)
{
if ( *first == '\0' || *second == '\0' )
break;

first++;
second++;
}
if( *first == '\0' && *second == '\0' )
return 0;
else
return -1;
}

so how do i get it to tell me which position the error starts??

thanx =)
post #2 of 13
Count the amount of "first++; second++;" you are doing in your compare function. Return that count instead of "-1".

To do that, start the compare function with:

int count = 1;

Then after those "first++; second++;" lines, add:

count++;

At the end of the compare function where you have "return -1;", change that into:

return count;
post #3 of 13
Thread Starter 
i tried but it doesnt show any difference
post #4 of 13
Well, you have to use the result in your current main() function. Where you show "bla bla not equal" on the screen, you need to change that into what your problem asks you to. You have that variable named "result" with the number and need to print that.

You might have made a mistake btw. That variable "result" in main() is of type "char" but should be "int".

You also forgot to do the "do you want to continue (y/n)" part of the problem. tongue.gif
post #5 of 13
Thread Starter 
currently my program looks like this...

#include



int compare_string(char*, char*);



int main()

{

char first[1000], second[1000], result;

char c;

int count;



do

{

{

printf("Enter first string\n");

scanf("%s" ,first);



printf("Enter second string\n");

scanf("%s" ,second);



result = compare_string(first, second);



if ( result == 0 )

{

printf("Both strings are the same.\n");

}

else

{

printf("Entered strings are not equal.\n");

printf( "The location of the error is at: %d\n", count);

}

}

{



printf( "Press 'y' to Continue or press 'n' to Exit.\n" );

scanf(" %c" , &c);

while ( !( c == 'y' || c == 'n' ))

{

printf( "Invalid Character Entered!\n" );

printf( "Please press 'y' to Continue or press 'n' to Exit.\n" );

scanf( " %c " , &c );

}

}

} while (c != 'n');

return 0;

}



int compare_string(char *first, char *second)

{

int count = 1;



while(*first==*second)

{

if ( *first == '\0' || *second == '\0' )

break;



*first++;

*second++;

count++;

}

if( *first == '\0' && *second == '\0' )

return 0;

else

return count;

}

i get everything except that the position of my error shows 0 always... sadsmiley.gif ...
post #6 of 13
What is the "#include" at the start for? What do you want to include? You forgot that.

The following is not technically a mistake, but you just got lucky and should correct it:

Don't write "*first++; *second++;". first and second are pointers. "*first" is the data at the pointer. You really want to do "first++". You are lucky that the ++ has higher priority than the *. Because of that higher priority, what you've written is really like this: "*(first++)". That's why it still works, but the * is still not something you want there.

This line:

char first[1000], second[1000], result;

Change it into:

char first[1000], second[1000];
int result;

This line:

printf( "The location of the error is at: %d\n", count);

You want to use "result" instead of "count". This is because you saved what "compare_string()" did in "result".

Delete the "int count;" line in main() as you don't use it in main().

Make "char c;" an array like for example "char c[100];". This is because functions like scanf() or gets() will change more than one character! They will at the least write a '\0' character tacked onto the end, so they will destroy data inside memory that does not belong to "char c". Don't forget to change "&c" into "c" after you made c an array, and use *c to compare to 'y' and 'n' instead of c.
post #7 of 13
Thread Starter 
ohh yeah i didnt copy the codes properly thts y the stdio.h is missing anyway thx 4 the help...the programs works... but it only works when the are no spaces in the input.... for example when i input "hello world" it has an error...it only works when i put "helloworld"
post #8 of 13
That's the scanf() you are using. I think if you do "scanf("%s", ...)", with that %s it searches for a single word and stops after the space character. I don't know how you can do it with scanf().

It will work if you instead use gets(). Like this:

printf("Enter first string\n");
gets(first);

printf("Enter second string\n");
gets(second);
post #9 of 13
Thread Starter 
ohh ok...thx 4 the help =)
post #10 of 13
This thread needs more [code] [/code] tags
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › c programming string comparison