<u>C program to check if two strings are different by one and only one character</u>
#include<stdio.h>
#include<string.h>
//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<l2;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");
}
<u>Output</u>
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