Ask Question

Write a program to check if two strings are different by one and only one character (when checked character by character). For example, lake and bake are different by one and only one character. pal and pale, bus and bit, kite and bit are NOT different by one and only one character.

+1
Answers (1)
  1. 16 March, 13:06
    0
    C program to check if two strings are different by one and only one character

    #include

    #include

    //driver function

    int main ()

    {

    int result;

    char f_string[100], s_string[100]; / * Declaring f_string and s_string as strings*/

    printf ("Enter the first string : / n"); / / Taking inputs from user

    scanf ("%s", f_string);

    printf ("Enter the second string : / n"); / /

    scanf ("%s", s_string);

    int l1 = strlen (f_string); / / calculating length of strings

    int l2 = strlen (s_string);

    int difference = 0; / / For storing the count difference in strings

    if (l1==l2) / / Checking lengths of string

    {

    for (int i=0; i
    {

    if (f_string[i]!=s_string[i]) / *checking each character of f_string with the same character index of s_string*/

    difference++; / / updating the count difference

    }

    }

    result=difference;

    if (result==1) / / if there is only one character difference

    printf ("The two strings are replaced by one character");

    else

    printf ("The two strings are not replaced by one character");

    }

    Output

    Enter the first string : lake

    Enter the second string : bake

    The two strings are replaced by one character

    Enter the first string : pal

    Enter the second string : pale

    The two strings are not replaced by one character

    Enter the first string : bus

    Enter the second string : bit

    The two strings are not replaced by one character

    Enter the first string : kite

    Enter the second string : bit

    The two strings are not replaced by one character
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Write a program to check if two strings are different by one and only one character (when checked character by character). For example, ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers