Comparing two letters in one string with another char string?

Thread Solved

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Comparing two letters in one string with another char string?

 
0
  #1
Dec 10th, 2006
Hi guys,

This exercise goes as follows:
- Write a function that counts the number of occurences of a pair of letters in a string and another that does the same in a zero-terminated array of char(a C-style string). For example, the pair "ab" appears twice in "xabaacbaxabb".

Now, I found the following solution to the C-style version:
  1. #include <iostream>
  2.  
  3. void compareChar(char *myArr, char *mySecArr);
  4.  
  5. int main()
  6. {
  7. char mychar[] = "xabaacbaxabb";
  8. char letters[2];
  9.  
  10. compareChar(mychar, letters);
  11.  
  12. std::cin.ignore(2);
  13.  
  14. return 0;
  15. }
  16.  
  17. void compareChar(char *myArr, char *mySecArr)
  18. {
  19. int count = 0;
  20.  
  21. std::cout << "Enter two letters to be compaired with the char string 'xabaacbaxabb'" << std::endl;
  22.  
  23. std::cin >> mySecArr;
  24.  
  25. while(strlen(mySecArr) != 2)
  26. {
  27. std::cout << "Wrong amount of letters, try again!" << std::endl;
  28. std::cin >> mySecArr;
  29. }
  30.  
  31. for (int i = 0; myArr[i] != 0; i++)
  32. {
  33. if((myArr[i] == mySecArr[0]) && (myArr[i+1] == mySecArr[1]))
  34. count++;
  35. }
  36.  
  37. std::cout << "The pair '" << mySecArr << "' appears " << count << " time(s) in " << myArr << "." << std::endl;
  38. }

I was wondering if any of you could tell me how I can improve this, the reason for asking is that at the end of the last comparison if((myArr[i] == mySecArr[0]) && (myArr[i+1] == mySecArr[1]))
the myArr[i+1] goes out of bound. Isn't that bad coding?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Comparing two letters in one string with another char string?

 
0
  #2
Dec 10th, 2006
Anything that goes beyond scope is naughty.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Comparing two letters in one string with another char string?

 
0
  #3
Dec 10th, 2006
Jobe, my very first reaction would be, why not write a generic function instead of restricting yourself with search strings of length 2 or 3.
goes out of bound. Isn't that bad coding?
Its not bad coding, its wrong coding. How can you expect your program to perform properly and in the right manner if you are basing your programming logic on computations on areas of memory which don't belong to you ?

Also the trick which you use will soon turn out to be cumbersome if you are asked to do the same with 3 letters, 4 letters, n letters etc.

My thoughts: always look for generic patterns in the problem statement and if they don't incur additional overheads for that case, go ahead and implement them.

For eg here is one implementation which is possible. I haven't tried it but it should pretty much work.
  1. // retuns the last position in the string where the match was found
  2. // modifies the count parameter passed by reference which displays the
  3. // number of times substring occurs.
  4. int my_strstr( const char* str1, const char* str2, int* count )
  5. {
  6. int i = 0 ;
  7. int len1 = strlen( str1 ) ;
  8. int len2 = strlen( str2 ) ;
  9. int pos = 0 ;
  10.  
  11. *count = 0 ; // initialize the counter
  12.  
  13. for( i = 0; i < len1; ++i )
  14. {
  15. if( strncmp( ( str1 + i ), str2, len2 ) == 0 )
  16. {
  17. (*count)++ ;
  18. pos = i ;
  19. }
  20. }
  21. return pos ;
  22. }
  23.  
  24. int main( void )
  25. {
  26. int count ;
  27. char str[] = "ababababa" ;
  28. char search_str[] = "ab" ;
  29. int match = my_strstr( str, search_str, &count ) ;
  30. printf( "The last occurance of \'%s\' was found in \'%s\' at %d.",
  31. str, search_str, match ) ;
  32. printf( "\nThe count of \'%s\' is %d.", search_str, count ) ;
  33. getchar( ) ;
  34.  
  35. return 0 ;
  36. }
You can change the function so that you get it to return the char pointer rather than returning position or you can make the function do both by keeping its prototype as:
char* my_strstr( const char* str1, const char* str2, int* count, int* pos )
which makes the function return char pointer to the substring found as well as modifies the parameters passed to it to reflect the count of substring as well as the position of the last substring.

Hope it helped, bye.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Comparing two letters in one string with another char string?

 
0
  #4
Dec 10th, 2006
Hmmm, well, I came up with this int strLength = strlen(myArr)-1; and then in the loop for (int i = 0; i < strLength; i++) but I see where you're coming from :o

Thanks for the example, I'll see that I can find an equivalent to use in my program next week-end.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Comparing two letters in one string with another char string?

 
0
  #5
Dec 10th, 2006
Originally Posted by JoBe View Post
Hmmm, well, I came up with this int strLength = strlen(myArr)-1; and then in the loop for (int i = 0; i < strLength; i++) but I see where you're coming from :o

Thanks for the example, I'll see that I can find an equivalent to use in my program next week-end.
I would reverse the -1 and go with:
int strLength = strlen(myArr); and for (int i = 0; i < strLength-1; i++)
The reason is strLength actually represents the string length, which you can modify in the loop. And you can still use strLength elsewhere in your program if the need arises. By subtracting 1 first, it's really only useful for this particular part of the program.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Comparing two letters in one string with another char string?

 
0
  #6
Dec 11th, 2006
Originally Posted by WaltP View Post
I would reverse the -1 and go with:
int strLength = strlen(myArr); and for (int i = 0; i < strLength-1; i++)
The reason is strLength actually represents the string length, which you can modify in the loop. And you can still use strLength elsewhere in your program if the need arises. By subtracting 1 first, it's really only useful for this particular part of the program.
Yep, that seems to make better sence, thanks Walt.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Comparing two letters in one string with another char string?

 
0
  #7
Dec 16th, 2006
Hi guys,

Solved the exercise, but, I'm not that happy with it because both functions actually look very similar, meaning, the c-style string and the cpp string. Don't think that was the idea of this exercise.

  1. //Exercises from the book: The C++ Programming Language by Bjarne Stroustrup.
  2.  
  3. /*Write a function that counts the number of occurences of a pair of letters in a string and
  4.   another that does the same in a zero-terminated array of char(a C-style string). For example,
  5.   the pair "ab" appears twice in "xabaacbaxabb".
  6.   */
  7.  
  8. #include <iostream>
  9. #include <string>
  10.  
  11. int compareChar(char *myArr, char *mySecArr);
  12. int compareString(std::string myWord, char mySecArr[2]);
  13.  
  14. int main()
  15. {
  16. char mychar[] = "xabaacbaxabba";
  17. char letters[2];
  18. std::string myWord;
  19.  
  20. myWord = mychar;
  21.  
  22. std::cout << "Enter two letters to be compaired with the char string '" << mychar << "'" << std::endl;
  23.  
  24. std::cin >> letters;
  25.  
  26. int count = compareChar(mychar, letters);
  27. std::cout << "The pair '" << letters << "' appears " << count << " time(s) in " << mychar << "." << std::endl;
  28.  
  29. std::cout << "Enter another two letter to be compaired with the string string '" << myWord << "'" << std::endl;
  30.  
  31. std::cin >> letters;
  32.  
  33. count = compareString(myWord, letters);
  34. std::cout << "The pair '" << letters << "' appears " << count << " time(s) in " << myWord << "." << std::endl;
  35.  
  36. std::cin.ignore(2);
  37.  
  38. return 0;
  39. }
  40.  
  41. int compareChar(char *myArr, char *mySecArr)
  42. {
  43. int count = 0;
  44.  
  45. for (int i = 0; i < strlen(myArr)-1; i++)
  46. {
  47. if((myArr[i] == mySecArr[0]) && (myArr[i+1] == mySecArr[1]))
  48. count++;
  49. }
  50.  
  51. return count;
  52. }
  53.  
  54. int compareString(std::string myWord, char mySecArr[2])
  55. {
  56. int count = 0;
  57.  
  58. for (int i = 0; i < myWord.size()-1; i++)
  59. {
  60. if ((myWord[i] == mySecArr[0]) && (myWord[i+1] == mySecArr[1]))
  61. count++;
  62. }
  63.  
  64. return count;
  65. }

Is there a better way to compare a character array with a cpp string?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,334
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 234
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Comparing two letters in one string with another char string?

 
0
  #8
Dec 16th, 2006
Maybe the C++ version was supposed to use .find()?

This is enough room for one character plus the terminating null:
char letters[2];
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Comparing two letters in one string with another char string?

 
0
  #9
Dec 16th, 2006
Originally Posted by JoBe View Post
Is there a better way to compare a character array with a cpp string?
There are other ways, but not necessarily better. For just two characters yours way is efficient. If you had to test 4 or more, a loop might be better.

There are also the C-string str???() functions and C++ strings compare methods. Either of those you can look into.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Comparing two letters in one string with another char string?

 
0
  #10
Dec 17th, 2006
Originally Posted by Dave Sinkula View Post
Maybe the C++ version was supposed to use .find()?
HI Dave, long time no see, I see you lost your mod stick, good to see you here again though

I tried using .find() and .compare(), but it seems that they can only be used to find if there is A resemblance of code in both strings, not several. Could it be that I have to write it in a way it traverses threw the string that I want to compare? If so, I can only traverse threw one by using a loop and either string[i] or *(string + i) right ?

This is enough room for one character plus the terminating null:
char letters[2];
DARN, I forgot the terminating null, it does work though, I'll change it to [3] and add the '\0' at the back, thanks for the tip.


Originally Posted by WaltP View Post
There are other ways, but not necessarily better. For just two characters yours way is efficient.
Oh, ok, thanks.

If you had to test 4 or more, a loop might be better.
Nope, don't understand that, I thought I was using a loop?

There are also the C-string str???() functions and C++ strings compare methods. Either of those you can look into.
Thought so, thing is, as said above, how do I use .find() and/or .compare() when having to go threw the whole string when trying to find more then one occurence of the same piece of string?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC