Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,194,196 members, 7,953,718 topics. Date: Friday, 20 September 2024 at 12:35 AM |
Nairaland Forum / Science/Technology / Programming / Help on C Programming (1717 Views)
C Programming Challenge To Be Solved Under 10 Minutes / Help On C# Multiple Form Application (2) (3) (4)
Help on C Programming by Polfire(m): 6:30am On Oct 03, 2011 |
Hi all, I am trying to find and read the occurrence of names after a string in a file example : "Mr James is here , Mr Paul , john and Andrew are on their way. Mr buss is also coming ." From the above sentence , i want to out the names after "Mr" i need help on how to do this. I will appreciate any help ,thx. #include <stdio.h> #include <stdlib.h> int main(int argc , char **argv[]) { FILE *input_file = fopen( argv[1], "r" ); /* open file */ , |
Re: Help on C Programming by ektbear: 5:24am On Oct 04, 2011 |
Blargh. Low level languages are so brutal. Conceptually, the way I'd do it is this: 1. Take the string, split it into an array of strings. 2. Loop over your array of strings, detect if the (i)th entry of your array equals the string "Mr", and if so output the (i+1)th string. This only works under the assumption that the string never has the word "Mr" as its last word, though! Here is the implementation in a high-level language (Ruby in this case): Gives me the following output:
So to build your own version of this in C, you basically need a function to split strings into array of strings. Looks like in C there is a function in library string.h that does this called "strtok." See this tutorial on how to use it: http://guy-lecky-thompson.suite101.com/string-tokenizing-in-c-programming-a9864 |
Re: Help on C Programming by ektbear: 5:41am On Oct 04, 2011 |
Actually I take back my statement about C being ugly for this. I implemented a version in C that does sort of what you are asking. . . sends output to STDOUT though rather than an array or another text file. Not too hard if you use this library function strtok. You'll find the function strcmp also handy. |
Re: Help on C Programming by Polfire(m): 5:47am On Oct 04, 2011 |
Thanks ekt_bear , let me work on this , I understand what you said , I will get back here if i am confuse in any way.Thanks |
Re: Help on C Programming by ektbear: 5:48am On Oct 04, 2011 |
No problem. If you google the phrase "string tokenizer c" you'll get lots of other resources too. Hrm, I like this article a lot: http://www.gnu.org/s/hello/manual/libc/Finding-Tokens-in-a-String.html It looks like strtok is a bit dangerous in that it modifies the string s in place. So just keep that in mind if you need to use your sentence again elsewhere. |
Re: Help on C Programming by Polfire(m): 5:55am On Oct 04, 2011 |
This is the problem , program that solve this , $ cat test1.txt I have just seen Sirs Sanjay and Eleonore! "I am a Knave," whispered Sir Eleonore. Who is a Knight and who is a Knave? $ solve $(cat test1.txt) The Sirs are: Eleonore Sanjay There is no solution. $ cat test2.txt I have just met Sirs Frank, Paul and Nina. Sir Nina said: "I am a Knight," but I am not sure if that is true. What do you think? $ solve $(cat test2.txt) The Sirs are: Frank Nina Paul There are 8 solutions. $ cat test3.txt Yesterday, I visited Sirs Andrew and Nancy. I asked Sir Andrew who he was, and he answered impatiently: "Sir Nancy and I are Knaves!" Then I met Sir Bill who introduced me to his wife and told me: "at least one of Sir Hilary and I is a Knave." Should I trust them? $ solve $(cat test3.txt) The Sirs are: Andrew Bill Hilary Nancy There is a unique solution: Sir Andrew is a Knave. Sir Bill is a Knight. Sir Hilary is a Knave. Sir Nancy is a Knight. |
Re: Help on C Programming by Polfire(m): 6:02am On Oct 04, 2011 |
I plan opening the file , fseeking it to get the size of the file and use the size to create an array that will contain the file characters char file_info[size] then I check for Sir or sirs using file-info[i]=='S' && file_info[i+1]=='I' && file_info[i+2]=='R' if that is true I will start returning file_info[i+4] till I reach a space . |
Re: Help on C Programming by ektbear: 7:22am On Oct 04, 2011 |
This new problem you pose is more complicated. |
Re: Help on C Programming by Fayimora(m): 5:38pm On Oct 04, 2011 |
Errrr if you have an arbitrary long file i think what you want to use is a Regex which would be very easy to construct, |
Re: Help on C Programming by Kobojunkie: 11:58pm On Oct 05, 2011 |
@Poster, in C, strtok will do that for you. I am guessing what you are trying to do is read the token directly after the word "Mr"? http://www.cplusplus.com/reference/clibrary/cstring/strtok/ |
(1) (Reply)
Please Recommend Simple Projects That A Beginner Can Start With / New Social Whot! and Snake Game For Android Phones / Java Certification: A Game Changer?
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 19 |