C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 10th, 2006, 04:23 PM
brad sue brad sue is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 41 brad sue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 49 m 31 sec
Reputation Power: 3
String problem

Hi , I need suggestion on how to do the following task in standard C:

I have a set of 5 cards like:

king of clubs
six of clubs
four of diamonds
six of hearts
four of hearts

I need read the 5 lines (strings) from a file, and count how many,words"six" and words "heart" I have in this set.


If I read the line with fgets, how can I discriminate the words"six" and "heart" from the whole line, for example?

I suspect pointers and the function strcmp but I dont know how to use them..
Please Can I have some indications?
Thank you for your help.
B

Reply With Quote
  #2  
Old March 11th, 2006, 01:17 AM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Quote:
Originally Posted by brad sue
Hi , I need suggestion on how to do the following task in standard C:

I have a set of 5 cards like:

king of clubs
six of clubs
four of diamonds
six of hearts
four of hearts

I need read the 5 lines (strings) from a file, and count how many,words"six" and words "heart" I have in this set.


If I read the line with fgets, how can I discriminate the words"six" and "heart" from the whole line, for example?

I suspect pointers and the function strcmp but I dont know how to use them..
Please Can I have some indications?
Thank you for your help.
B


Either you repeatedly use strcmp funtion to compare "six" and/or "hearts" in each of the strings or use strstr funtion.

The strstr function searches a substrig in a string.

Reply With Quote
  #3  
Old March 11th, 2006, 01:23 AM
brad sue brad sue is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 41 brad sue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 49 m 31 sec
Reputation Power: 3
Quote:
Originally Posted by Cirus
Either you repeatedly use strcmp funtion to compare "six" and/or "hearts" in each of the strings or use strstr funtion.

The strstr function searches a substrig in a string.


Thank you very much!
I have an issue with the file reading.
for example ,we have the same set of cards but before it, there is an int like:

12
king of clubs
six of clubs
four of diamonds
six of hearts
four of hearts

If I use fgets and fputs to read and print the file, what can I do so that I star reading and printfrom the second line( skip the line with 12)

I called each line LINE and set a while loop

while( 1 ) {/*while begins*/

fgets(LINE, sizeof(LINE), fin);

if ( feof(fin) ) /* check for EOF right after fgets() */
break;
fputs(LINE, fout);}


I dont succeed to skip the first line I tried LINE+1 but it is an horizontal displacement..
Can I have some suuggestions?

Reply With Quote
  #4  
Old March 11th, 2006, 01:33 AM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
The 12 in your file is treated as "12" b the function.Hence its not allowing you to skip it.

But even after that it is solving your purpose sice strstr won't find token "siz" or"hearts" there.

However if you wish to skip such string. Use strrtok ( string tokenizer) then convert string to integer. There are funcitons available in VC++ and VB that converts string to integer.Or you can perhaps use atoi() .

Then check whether its an integer or not.

Or another solution is to check the ASCII value of first character of each line.Look, I am assuming in this case that a line will be read on the basis of ASCII value of first character of the string.

For 1 it is 49 for 0 its 48 .

But if you want to get rid of a string that has a number in between that is : "Hi how are 12 you\n", then there can be a problem.

Quote:
Originally Posted by brad sue
Thank you very much!
I have an issue with the file reading.
for example ,we have the same set of cards but before it, there is an int like:

12
king of clubs
six of clubs
four of diamonds
six of hearts
four of hearts

If I use fgets and fputs to read and print the file, what can I do so that I star reading and printfrom the second line( skip the line with 12)

I called each line LINE and set a while loop

while( 1 ) {/*while begins*/

fgets(LINE, sizeof(LINE), fin);

if ( feof(fin) ) /* check for EOF right after fgets() */
break;
fputs(LINE, fout);}


I dont succeed to skip the first line I tried LINE+1 but it is an horizontal displacement..
Can I have some suuggestions?

Reply With Quote
  #5  
Old March 11th, 2006, 01:39 AM
brad sue brad sue is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 41 brad sue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 49 m 31 sec
Reputation Power: 3
Quote:
Originally Posted by Cirus
The 12 in your file is treated as "12" b the function.Hence its not allowing you to skip it.

But even after that it is solving your purpose sice strstr won't find token "siz" or"hearts" there.

However if you wish to skip such string. Use strrtok ( string tokenizer) then convert string to integer. There are funcitons available in VC++ and VB that converts string to integer.Or you can perhaps use atoi() .

Then check whether its an integer or not.

Or another solution is to check the ASCII value of first character of each line.Look, I am assuming in this case that a line will be read on the basis of ASCII value of first character of the string.

For 1 it is 49 for 0 its 48 .

But if you want to get rid of a string that has a number in between that is : "Hi how are 12 you\n", then there can be a problem.


OK
thank you, I'll try strrtok.

Reply With Quote
  #6  
Old March 12th, 2006, 03:30 AM
brad sue brad sue is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2006
Posts: 41 brad sue User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 49 m 31 sec
Reputation Power: 3
Quote:
Originally Posted by Cirus
The 12 in your file is treated as "12" b the function.Hence its not allowing you to skip it.

But even after that it is solving your purpose sice strstr won't find token "siz" or"hearts" there.

However if you wish to skip such string. Use strrtok ( string tokenizer) then convert string to integer. There are funcitons available in VC++ and VB that converts string to integer.Or you can perhaps use atoi() .

Then check whether its an integer or not.

Or another solution is to check the ASCII value of first character of each line.Look, I am assuming in this case that a line will be read on the basis of ASCII value of first character of the string.

For 1 it is 49 for 0 its 48 .

But if you want to get rid of a string that has a number in between that is : "Hi how are 12 you\n", then there can be a problem.


I have a serious problem to understand to code a task.

we have a set of 5 cards like:

nine of hearts
queen of hearts
ten of hearts
jack of hearts
king of hearts


we are ask to code the task according to the following characteristic:

straight flush (5 cards in sequence*, all of the same suit) has a value of 10 points.

*sequence is 2,3,4,5,6,7,8,9,10,jack,queen,king with ace considered as either before 2 or after king as needed.


The set of cards above has a value of 10 pts.
I have suceeded to count the number of suits(with strstr and a counting loop) , but how can I work out the sequence part??
I have no idea how to start here.
Can help me please?
B

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > String problem


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT