using strcmp

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 2
Reputation: cmsc21_scared is an unknown quantity at this point 
Solved Threads: 0
cmsc21_scared cmsc21_scared is offline Offline
Newbie Poster

using strcmp

 
0
  #1
Aug 2nd, 2009
* Our focus here is on STRINGS since they are arrays of characters.


* Write a program that reads a line of text (possibly a sentence therefore you can't use scanf() here), counts the number of words in it,
and displays the words.

* For our purposes, we define a word simply as a sequence of non-space characters.

Example:

Enter text: Roses are red, violets are blue.

The text has 6 words. These are

Roses
are
red,
violets
are
blue.

(In orange are the user's input)

* It includes punctuation marks and also counts duplicate words. You are not required to avoid these
but you may try creating a program that avoids counting duplicate words. You would receive extra 5 points
for doing so (the exercise alone is worth 10 points). You might need to use a two-dimensional array of
characters to do this and you will need to use strcmp().



help???

how do u solve this???
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: using strcmp

 
0
  #2
Aug 2nd, 2009
Please read the "sticky" posts at the head of this forum.

Show some effort (even just a liitle)!

http://www.daniweb.com/forums/announcement118-2.html
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 2
Reputation: cmsc21_scared is an unknown quantity at this point 
Solved Threads: 0
cmsc21_scared cmsc21_scared is offline Offline
Newbie Poster

Re: using strcmp

 
0
  #3
Aug 2nd, 2009
i actually already have a program for a part of that..the only problem now is how to count the number of words without counting the repeating words using strcmp...
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 201
Reputation: yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold yellowSnow is a splendid one to behold 
Solved Threads: 35
yellowSnow's Avatar
yellowSnow yellowSnow is offline Offline
Posting Whiz in Training

Re: using strcmp

 
0
  #4
Aug 2nd, 2009
Originally Posted by cmsc21_scared View Post
i actually already have a program for a part of that..the only problem now is how to count the number of words without counting the repeating words using strcmp...
Code talks .. blah blah blah walks!
Manic twiddler of bits
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 60
Reputation: Nogat21 is an unknown quantity at this point 
Solved Threads: 0
Nogat21 Nogat21 is offline Offline
Junior Poster in Training

Re: using strcmp

 
0
  #5
Aug 21st, 2009
i'm with a question related to this...i'm trying to compare two strings (well char pointers in fact ), im using _stricmp for case insensitive comparison, but even with the conventional strcmp i'm not getting the result i want.

  1. if(_stricmp(value,gen_arr[i])== 0){
  2.  
  3. file.seekp(-1,ios::end);
  4. file.put((i));
  5. file.flush();
  6. file.close();
  7. printf("Editing of field %s successful.\n",field);
  8. return;
  9. }

I'm testing this with two "strings", the first coming from the argument line (value) and the other from a char * array (thats it, an array of strings) (gen_arr) and i'm setting value with a known value in the array.

I'm doing some "print" debugging paralel with the proper debug, and the strings are correctly set, they are equal. But the comparison says otherwise. Can you guys give me a hand here?
Thanks, and sorry for taking advantage of another thread for this, i thought it would help the original poster

P.S: Oh crap, now i realised this is the C forum...but can u guys still help me? Thanks a lot
Last edited by Nogat21; Aug 21st, 2009 at 8:30 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,452
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: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: using strcmp

 
0
  #6
Aug 21st, 2009
I don't know that you've posted enough code to debug. How about a compilable snippet that demonstrates the problem?
"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: Jul 2009
Posts: 60
Reputation: Nogat21 is an unknown quantity at this point 
Solved Threads: 0
Nogat21 Nogat21 is offline Offline
Junior Poster in Training

Re: using strcmp

 
0
  #7
Aug 21st, 2009
  1. char * gen_arr[] = {
  2. "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge",
  3. "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B",
  4. "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska",
  5. "Death Metal", "Pranks", "Soundtrack"};
  6.  
  7. void Mp3::editTag(const char * field, const char * value){
  8.  
  9. const char * nome = this->Getname();
  10. fstream file(nome, ios::out | ios::in | ios::binary);
  11.  
  12. char newValue[30] = "Country";
  13. char field[30] = "genre"; //for the sake of this example
  14. int i = 0;
  15.  
  16. if(_stricmp(field,"genre") == 0){
  17.  
  18. //strcpy(newValue,value);
  19. cout<<value<<endl;
  20.  
  21. while(gen_arr[i]!=0){
  22.  
  23. cout<<gen_arr[i]<<endl;
  24. cout<<strcmp(value,gen_arr[i])<<endl;
  25.  
  26. if(_stricmp(value,gen_arr[i])== 0){
  27.  
  28. file.seekp(-1,ios::end);
  29. file.put((i));
  30. file.flush();
  31. file.close();
  32. printf("Editing of field %s successful.\n",field);
  33. return;
  34. }
  35. i++;
  36. }
  37. strcpy(newValue,value);
  38. gen_arr[i] = newValue;
  39. file.seekp(-1,ios::end);
  40. file.put(i);
  41. file.flush();
  42. file.close();
  43. printf("Editing of field %s successful.\n",field);
  44. return;
  45. }
  46. else{
  47. printf("ERROR: Wrong field specified.\n");
  48. return;
  49.  
  50. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,452
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: 250
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: using strcmp

 
0
  #8
Aug 21st, 2009
Is this close enough?
  1. #include <iostream>
  2. #include <string.h>
  3. using std::cout;
  4. using std::endl;
  5.  
  6. char * gen_arr[] = {
  7. "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge",
  8. "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B",
  9. "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska",
  10. "Death Metal", "Pranks", "Soundtrack",NULL
  11. };
  12.  
  13. void editTag(const char * field, const char * value)
  14. {
  15. if ( strcmp(field,"genre") == 0 )
  16. {
  17. cout << "value = " << value << endl;
  18.  
  19. for ( int i = 0; gen_arr[i] != 0; ++i )
  20. {
  21. cout << " gen_arr[" << i << "] = " << gen_arr[i] << endl;
  22. if ( strcmp(value, gen_arr[i]) == 0 )
  23. {
  24. cout << "found!!!\n";
  25. return;
  26. }
  27. }
  28. puts("fail");
  29. }
  30. else
  31. {
  32. printf("ERROR: Wrong field specified.\n");
  33. }
  34. }
  35.  
  36. int main()
  37. {
  38. editTag("genre","Country");
  39. cout << "---\n";
  40. editTag("genre","xxx");
  41. return 0;
  42. }
  43.  
  44. /* my output
  45. value = Country
  46.   gen_arr[0] = Blues
  47.   gen_arr[1] = Classic Rock
  48.   gen_arr[2] = Country
  49. found!!!
  50. ---
  51. value = xxx
  52.   gen_arr[0] = Blues
  53.   gen_arr[1] = Classic Rock
  54.   gen_arr[2] = Country
  55.   gen_arr[3] = Dance
  56.   gen_arr[4] = Disco
  57.   gen_arr[5] = Funk
  58.   gen_arr[6] = Grunge
  59.   gen_arr[7] = Hip-Hop
  60.   gen_arr[8] = Jazz
  61.   gen_arr[9] = Metal
  62.   gen_arr[10] = New Age
  63.   gen_arr[11] = Oldies
  64.   gen_arr[12] = Other
  65.   gen_arr[13] = Pop
  66.   gen_arr[14] = R&B
  67.   gen_arr[15] = Rap
  68.   gen_arr[16] = Reggae
  69.   gen_arr[17] = Rock
  70.   gen_arr[18] = Techno
  71.   gen_arr[19] = Industrial
  72.   gen_arr[20] = Alternative
  73.   gen_arr[21] = Ska
  74.   gen_arr[22] = Death Metal
  75.   gen_arr[23] = Pranks
  76.   gen_arr[24] = Soundtrack
  77. fail
  78. */
Last edited by Dave Sinkula; Aug 21st, 2009 at 10:02 pm. Reason: More editing of snippet.
"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  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC