Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,231 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,853 people online right now. Registration is fast and FREE... Join Now!




Comparing strings

 
Reply to this topicStart new topic

Comparing strings, comparision of strings

martina
13 Mar, 2007 - 03:18 AM
Post #1

New D.I.C Head
*

Joined: 13 Mar, 2007
Posts: 7


My Contributions
Hi!

Need some help in writing a code in C!

I want to compare two strings, if they both contain any (at least) 4-letter substring which is the same.
For example:
'description' and 'crippled' both contain the same substring 'crip'.

Can I use here strstr() anyhow?

Many thanks to anyone posting an answer!

Best,
Martina
User is offlineProfile CardPM
+Quote Post

AmitTheInfinity
RE: Comparing Strings
13 Mar, 2007 - 03:45 AM
Post #2

C Surfing ∞
Group Icon

Joined: 25 Jan, 2007
Posts: 1,026



Thanked: 35 times
Dream Kudos: 125
My Contributions
QUOTE(martina @ 13 Mar, 2007 - 04:48 PM) *

Hi!

Need some help in writing a code in C!

I want to compare two strings, if they both contain any (at least) 4-letter substring which is the same.
For example:
'description' and 'crippled' both contain the same substring 'crip'.

Can I use here strstr() anyhow?

Many thanks to anyone posting an answer!

Best,
Martina


well, I don't think that strstr will be useful in this case.
It would be better to write a function for that.
Writing a logic for it would not be that difficult.

All you need to do is something like this :

CODE


found=-1;
for(i=0,j=0;i<strlen(word1);)
{
  if(word1[i]==world2[j])
  {
    k=i;
    l=j;
    count=0;
    while(k<strlen(word1) && l<strlen(word2) && word1[k++]==word2[l++]) count++;
    if(count>=4)
    { found = i; break; }
   }
   j++;
   if(j==strlen(word2))
   { i++; j=0; }  
}
if(found>=0)
{
  for(i=found;i<count;i++)
    printf("%c",word1[i]);
}
else
  printf("No common pattern found.");



Please note that I wrote it on the fly. This code is not tested even not compiled also. So please take the logic out of it and make your code.

Hope this will help you. smile.gif
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Comparing Strings
13 Mar, 2007 - 04:24 AM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
You can use strstr(), but only if you know the substring you're looking for in advance. You can run the strstr() function on both strings, looking for the specified substring - neither call returns NULL, the same substring exists.Again, this method is dependent on knowing the sub string in advance.
User is online!Profile CardPM
+Quote Post

martina
RE: Comparing Strings
20 Mar, 2007 - 05:38 AM
Post #4

New D.I.C Head
*

Joined: 13 Mar, 2007
Posts: 7


My Contributions
Thanks a lot for your answers!

I already got it to work with strstr() inside, maybe it's not so nice code as yours, AmitTheInfinity, but it works anyway.

Just used the way to compare any 4 letter-substrings in one word with another, taking all 4 letter substrings from the shorter word and comparing them with strstr() to another.
Huh, twas difficult explanation, wasn't it, haha?

And I think your code was really good, Amit!

smile.gif

Best!

Martina
User is offlineProfile CardPM
+Quote Post

imamkomc
RE: Comparing Strings
9 May, 2007 - 03:45 AM
Post #5

D.I.C Head
Group Icon

Joined: 9 May, 2007
Posts: 62


Dream Kudos: 225
My Contributions
Hi!

Need some help in writing a code in C!

I want to mixe two strings to one strings
For example:
'like' and 'look' then 'likelook'.

Thank you very much
imamkomc

User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Comparing Strings
9 May, 2007 - 04:13 AM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,230



Thanked: 40 times
Dream Kudos: 25
My Contributions
If you are using C, then check out the strcat() function for your needs.
User is online!Profile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 03:24PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month