| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
Quote:
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. |
|
#3
|
|||
|
|||
|
Quote:
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? |
|
#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:
|
|
#5
|
|||
|
|||
|
Quote:
OK thank you, I'll try strrtok. |
|
#6
|
|||
|
|||
|
Quote:
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 |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > String problem |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|