Need help with my program it does not repeat properly it doesn't wait for the input after you press y to continue instead it like the below
Output of the program:
Enter first string
s
Enter second string
s
Both strings are the same.
Press 'y' to Continue or press 'n' to Exit. (problem is here when i press y it repeats without asking for the input)
y
Enter first string
Enter second string
Both strings are the same.
Press 'y' to Continue or press 'n' to Exit.
Code:
Output of the program:
Enter first string
s
Enter second string
s
Both strings are the same.
Press 'y' to Continue or press 'n' to Exit. (problem is here when i press y it repeats without asking for the input)
y
Enter first string
Enter second string
Both strings are the same.
Press 'y' to Continue or press 'n' to Exit.
Code:
Code:
#include<stdio.h>
int compare_string(char*, char*);
int main()
{
char first[1000], second[1000], result;
char c;
do
{
{
printf("Enter first string\n");
scanf("%[^\n]%*c", first);
printf("Enter second string\n");
scanf("%[^\n]%*c", 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", result);
}
}
{
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;
}