find the common prefix of two strings

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cctype>	
#include <cstring>
#include <cstdlib>
using namespace std;

void prefix(const char s1[], const char s2[], char commonPrefix[])
{
	for(int i=0; s2[i] != '\0'; i++)
   {
      if(s2[i] == s1[i])
      {
         cout << s1;
      }
   }

}


int main()
{
	char string1[256];
	char string2[256];
	char Prefix[256];
	cout << "Enter a string s1: ";
	cin.getline(string1, 256);
	cout << "Enter a string s2: ";
	cin.getline(string2, 256);
	prefix(string1,string2,Prefix);

	return 0;
	}

	
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]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void prefix(const char s1[], const char s2[], char commonPrefix[])
{
	commonPrefix = '0';
for(int i=0; s2[i] && s1[i] != '\0'; i++)
   {
      while(s2[i] == s1[i])
      {
		  s1[i] = commonPrefix[i];
		  
		 
	  }
	  cout << commonPrefix[i];
   }


}


Like this? but wrong also.
This is my second try but i get the output 0.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void prefix(const char s1[], char s2[], char commonPrefix[])
{
	int cpi = 0;
	commonPrefix[cpi] = '0';
for(int i=0; s2[i] && s1[i] != '\0'; i++)
   {
      while(s2[i] == s1[i])
      {
		 s2[i] = commonPrefix[cpi];
		  
		 
	  }
	 
   }
 cout << 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(const char s1[], const char s2[], char commonPrefix[])
{
    int cpi = 0;
    int i = 0;

    while( (s2[i] && s1[i]) && (s2[i] == s1[i]))
   {
        commonPrefix[cpi++] = s1[i++];
   }
}
Like this?
i should cout commonPrefix[cpi} right?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void prefix(const char s1[], const char s2[], char commonPrefix[])
{
	int cpi = 0;
for(int i=0; s2[i] && s1[i] != '\0'; i++)
   {
     while( (s2[i] && s1[i]) && (s2[i] == s1[i]))
      {
		 commonPrefix[cpi++] = s1[i++];
		  
		 
	  }
	 cout << commonPrefix[cpi];
   }


}
1
2
3
4
5
void common_prefix( const char a[], const char b[], char prefix[] ) // C
{
    for( ; *a && *b && *a == *b ; ++a, ++b ) *prefix++ = *a ;
    *prefix = 0 ; // null terminate
}


1
2
3
4
5
6
7
8
#include <string>
#include <algorithm>

std::string common_prefix( std::string a, std::string b ) // C++
{
    if( a.size() > b.size() ) std::swap(a,b) ;
    return std::string( a.begin(), std::mismatch( a.begin(), a.end(), b.begin() ).first ) ;
}
Can you correct the above code for me?
> Can you correct the above code for me?

I haven't tested it, but I can't spot an obvious error in either the C or the C++ code.
i'm just a beginner to c++.
I don't understand your code.
can you make it simple?
1
2
 for( ; *a && *b && *a == *b ; ++a, ++b ) *prefix++ = *a ;
    *prefix = 0 ; // null terminate 
Would this be simpler?
1
2
3
4
5
6
7
void common_prefix( const char a[], const char b[], char prefix[] ) // C
{
    int i = 0  ;
    for( ; a[i] != 0 /* not a null char */ && a[i] == b[i] /* chars are equal */ ; ++i )
           prefix[i] = a[i] ; // copy char to prefix
    prefix[i] = 0 ; // null terminate prefix
}
YES thank you ! =)
Topic archived. No new replies allowed.