I need to write a program to find the common prefix of two strings.
for example.
Enter string 1: I love you.
Enter string 2: I love myself;
The common prefix is "I love".
This is my code so far. I have no idea what to put inside the for loop.
Please help.
In the for loop
1) you will need a variable declared and initialized to zero before "for" for index of commonPrefix
2) change the condition s2[i] != '\0'; to check if s1 has reached NULL as well. Otherwise in case s1 is smaller than s2 this will fail
3) change if(s2[i] == s1[i]) to while(s2[i] == s1[i])
4) inside the while loop assign s1[i] to commonPrefix[cpi]
Thats wrong code
in 1) I said a variable for index of commonPrefix not commonPrefix initialised to zero
in 4) assign s1[i] to commonPrefix[cpi] meaning commonPrefix[cpi] = spy[i] not the other way. This was also hint that you declare cpi as index for commonPrefix
Sorry for the confusion, here
1 2 3 4 5 6 7 8 9 10
void prefix(constchar s1[], constchar s2[], char commonPrefix[])
{
int cpi = 0;
int i = 0;
while( (s2[i] && s1[i]) && (s2[i] == s1[i]))
{
commonPrefix[cpi++] = s1[i++];
}
}