* 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???

Recommended Answers

All 7 Replies

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...

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!

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.

if(_stricmp(value,gen_arr[i])== 0){

							file.seekp(-1,ios::end);
								file.put((i));
								file.flush();
								file.close();
								printf("Editing of field %s successful.\n",field);
								return;
						}

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 ;)

I don't know that you've posted enough code to debug. How about a compilable snippet that demonstrates the problem?

char * gen_arr[] = {
	"Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge",
	"Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B",
	"Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska",
	"Death Metal", "Pranks", "Soundtrack"};

void Mp3::editTag(const char * field, const char * value){

const char * nome = this->Getname();
			fstream file(nome, ios::out | ios::in | ios::binary);

			char newValue[30] = "Country";
			char field[30] = "genre"; //for the sake of this example
			int i = 0;

if(_stricmp(field,"genre") == 0){

					//strcpy(newValue,value);
					cout<<value<<endl;

					while(gen_arr[i]!=0){

						cout<<gen_arr[i]<<endl;
						cout<<strcmp(value,gen_arr[i])<<endl;

						if(_stricmp(value,gen_arr[i])== 0){

							file.seekp(-1,ios::end);
								file.put((i));
								file.flush();
								file.close();
								printf("Editing of field %s successful.\n",field);
								return;
						}	
						i++;
			}
					strcpy(newValue,value);
					gen_arr[i] = newValue;
					file.seekp(-1,ios::end);
					file.put(i);
					file.flush();
					file.close();
					printf("Editing of field %s successful.\n",field);
					return;
				}
			else{
				printf("ERROR: Wrong field specified.\n");
				return;

			}

Is this close enough?

#include <iostream>
#include <string.h>
using std::cout;
using std::endl;

char * gen_arr[] = {
   "Blues", "Classic Rock", "Country", "Dance", "Disco", "Funk", "Grunge",
   "Hip-Hop", "Jazz", "Metal", "New Age", "Oldies", "Other", "Pop", "R&B",
   "Rap", "Reggae", "Rock", "Techno", "Industrial", "Alternative", "Ska",
   "Death Metal", "Pranks", "Soundtrack",NULL
   };

void editTag(const char * field, const char * value)
{
   if ( strcmp(field,"genre") == 0 )
   {
      cout << "value = " << value << endl;

      for ( int i = 0; gen_arr[i] != 0; ++i )
      {
         cout << "  gen_arr[" << i << "] = " << gen_arr[i] << endl;
         if ( strcmp(value, gen_arr[i]) == 0 )
         {
            cout << "found!!!\n";
            return;
         }
      }
      puts("fail");
   }
   else
   {
      printf("ERROR: Wrong field specified.\n");
   }
}

int main()
{
   editTag("genre","Country");
   cout << "---\n";
   editTag("genre","xxx");
   return 0;
}

/* my output
value = Country
  gen_arr[0] = Blues
  gen_arr[1] = Classic Rock
  gen_arr[2] = Country
found!!!
---
value = xxx
  gen_arr[0] = Blues
  gen_arr[1] = Classic Rock
  gen_arr[2] = Country
  gen_arr[3] = Dance
  gen_arr[4] = Disco
  gen_arr[5] = Funk
  gen_arr[6] = Grunge
  gen_arr[7] = Hip-Hop
  gen_arr[8] = Jazz
  gen_arr[9] = Metal
  gen_arr[10] = New Age
  gen_arr[11] = Oldies
  gen_arr[12] = Other
  gen_arr[13] = Pop
  gen_arr[14] = R&B
  gen_arr[15] = Rap
  gen_arr[16] = Reggae
  gen_arr[17] = Rock
  gen_arr[18] = Techno
  gen_arr[19] = Industrial
  gen_arr[20] = Alternative
  gen_arr[21] = Ska
  gen_arr[22] = Death Metal
  gen_arr[23] = Pranks
  gen_arr[24] = Soundtrack
fail
*/
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.