| | |
using strcmp
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2009
Posts: 2
Reputation:
Solved Threads: 0
* 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???
* 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???
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
Show some effort (even just a liitle)!
http://www.daniweb.com/forums/announcement118-2.html
Manic twiddler of bits
•
•
Join Date: Jul 2009
Posts: 60
Reputation:
Solved Threads: 0
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.
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
), im using _stricmp for case insensitive comparison, but even with the conventional strcmp i'm not getting the result i want. cpp Syntax (Toggle Plain Text)
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
Last edited by Nogat21; Aug 21st, 2009 at 8:30 pm.
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
•
•
Join Date: Jul 2009
Posts: 60
Reputation:
Solved Threads: 0
cpp Syntax (Toggle Plain Text)
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?
C Syntax (Toggle Plain Text)
#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 */
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
![]() |
Similar Threads
- no matching function for call to 'strcmp(std::string&, std::string&)' (C++)
- Can somebody explain to me about the 'strcmp' (C)
- C++ strcmp (C++)
- strcmp of 2-D array (C)
- need info? can't use strcmp/string compare (C++)
Other Threads in the C Forum
- Previous Thread: Command line arguments
- Next Thread: Time validation code
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays bash binarysearch changingto char character cm command copyanyfile copypdffile createprocess() database directory drawing dynamic execv feet fgets file floatingpointvalidation fork framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping loopinsideloop. lowest matrix meter microsoft mqqueue oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power process program programming pyramidusingturboccodes read recursion recv recvblocked reversing segmentationfault single socket socketprogramming spoonfeeding standard strchr string student suggestions system test testing threads unix urboc user whythiscodecausesegmentationfault win32api windowsapi






