Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,154,743 members, 7,824,143 topics. Date: Saturday, 11 May 2024 at 12:28 AM

C Programming Challenge To Be Solved Under 10 Minutes - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / C Programming Challenge To Be Solved Under 10 Minutes (3751 Views)

Programming Challenge For Beginners Competition Two N20000 -SEASON 2- / Programming Challenge For Beginners N20000 / Facebook Programming Challenge Question (2) (3) (4)

(1) (Reply) (Go Down)

C Programming Challenge To Be Solved Under 10 Minutes by logic101: 3:22pm On Oct 16, 2011
Write a function with the following prototype:
int tokenise(char str[], int start, char result[]);

The function should find the next token starting at index 'start'
in the string 'str'. The token should be copied into the string
'result', which is another character array passed to the function,
so that the string is communicated back to the caller.

The function returns an integer value, which should be the next
position in 'str' after the returned token. Next time the function is
called, this will be the next value of start. If there are no more
tokens, then the returned integer value should be -1.

You might use this function as follows:
char line[] = "Nothing but the rain";
char result[256];
char start;

start = tokenise(line, 0, result);
while ( start != -1 ) {
printf("%s\n", result);
start = tokenise(line, start, result);
}
Re: C Programming Challenge To Be Solved Under 10 Minutes by dellnet: 6:38pm On Oct 16, 2011
int tokenise(char str[], int start, char result[])
Re: C Programming Challenge To Be Solved Under 10 Minutes by Nobody: 6:52pm On Oct 16, 2011
Re: C Programming Challenge To Be Solved Under 10 Minutes by dellnet: 6:57pm On Oct 16, 2011
Yes despite my disk is out a * is the way.
Re: C Programming Challenge To Be Solved Under 10 Minutes by Nobody: 7:40pm On Oct 16, 2011
Re: C Programming Challenge To Be Solved Under 10 Minutes by dellnet: 7:43pm On Oct 16, 2011
Well, post your compiler if it works.
Re: C Programming Challenge To Be Solved Under 10 Minutes by dellnet: 7:47pm On Oct 16, 2011
logic101:

Write a function with the following prototype:
int tokenise(char str[], int start, char result[]);

The function should find the next token starting at index 'start'
in the string 'str'. The token should be copied into the string
'result', which is another character array passed to the function,
so that the string is communicated back to the caller.

The function returns an integer value, which should be the next
position in 'str' after the returned token. Next time the function is
called, this will be the next value of start. If there are no more
tokens, then the returned integer value should be -1.

You might use this function as follows:
char line[] = "Nothing but the rain";
char result[256];
char start;

start = tokenise(line, 0, result);
while ( start != -1 ) {
  printf("%s\n", result);
  start = tokenise(line, start, result);
}
Re: C Programming Challenge To Be Solved Under 10 Minutes by Nobody: 10:18pm On Oct 16, 2011
Re: C Programming Challenge To Be Solved Under 10 Minutes by Nobody: 10:20pm On Oct 16, 2011
Re: C Programming Challenge To Be Solved Under 10 Minutes by Nobody: 10:22pm On Oct 16, 2011
Re: C Programming Challenge To Be Solved Under 10 Minutes by Nobody: 10:23pm On Oct 16, 2011
Re: C Programming Challenge To Be Solved Under 10 Minutes by logic101: 12:22am On Oct 17, 2011
9ice work omo to dun,
Re: C Programming Challenge To Be Solved Under 10 Minutes by Nobody: 12:31am On Oct 17, 2011
Re: C Programming Challenge To Be Solved Under 10 Minutes by logic101: 1:36am On Oct 17, 2011
your welcome omo to dun , something more interesting hopefully

Once you have this working, modify the function so that there
are checks on the maximum lengths of the strings to ensure that
you don't go over the ends of the array:

int tokenise(char str[], int max_str, int start,
char result[], int max_result);

If the length of a token to be returned is longer than will fit in
'result', the token should be truncated.
Re: C Programming Challenge To Be Solved Under 10 Minutes by Nobody: 2:22am On Oct 17, 2011
Re: C Programming Challenge To Be Solved Under 10 Minutes by dellnet: 6:03am On Oct 17, 2011
omo_to_dun:

So dell_net, what was wrong with the prototype?
The OP said to pass char to int and you changed it to int before passing.

You might use this function as follows:
char line[] = "Nothing but the rain";
char result[256];
char start;

start = tokenise(line, 0, result);
Re: C Programming Challenge To Be Solved Under 10 Minutes by Nobody: 7:28am On Oct 17, 2011
Re: C Programming Challenge To Be Solved Under 10 Minutes by Nobody: 7:40am On Oct 17, 2011

Re: C Programming Challenge To Be Solved Under 10 Minutes by dellnet: 11:39am On Oct 17, 2011
I think C is easier than C++. Anyways in the long run char is limited to 255 if unsigned so all I am saying is . . . well, if the programming challenge is to tokenize small strings then the OP's concept is correct however I like to program in a standard way to avoid any leaks.

try this with start as int and with start as char and see what I mean
 char line[] = "If I had used char, the result would have been the same."
"Since the prototype is int, the char would be promoted to int because "
"the number of bits required to represent the latter is larger than that to represent the former. "
"If you don't believe me, run my code, with the int changed to char."
"For other doubting Thomases and C newbies, let me take the pain of showing it to you, again:";
Re: C Programming Challenge To Be Solved Under 10 Minutes by Nobody: 12:00pm On Oct 17, 2011
Re: C Programming Challenge To Be Solved Under 10 Minutes by dellnet: 12:22pm On Oct 17, 2011
Yes very nice concept indeed, small but yet powerful. I learnt something from this challenge.
Re: C Programming Challenge To Be Solved Under 10 Minutes by brownieevelyn: 6:53pm On Oct 31, 2011
somebody,,,, teach me somethingggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg
Re: C Programming Challenge To Be Solved Under 10 Minutes by Nobody: 9:46pm On Oct 31, 2011
@OP I ain't doing your home work for you nyukka  undecided . . .


@all, I know it is the pride of a geek to answer questions posed at them, but really, let's leave folks to do their assignments themselves.
Not everyone should be a programmer TBH undecided
Re: C Programming Challenge To Be Solved Under 10 Minutes by Slyr0x: 12:47pm On Nov 01, 2011
[center]Omo to dun

You should have at least allowed this thread to enter its 2nd page before proffering solution now  angry . .considering the fact that Seun had to put it on NL homepage just to make sure lotsa people solve it. .

Omo to dun saves the day. .[size=3pt]as usual[/size]   cheesy

[img]http://t0.gstatic.com/images?q=tbn:ANd9GcSL-bjUG1sWfyF-L_QZJ-zEYeGREDVqAJTDcj9vnj3xoO4A2-1w&t=1[/img][/center]
Re: C Programming Challenge To Be Solved Under 10 Minutes by dhtml1(m): 9:17pm On Nov 01, 2011
omo_to_dun - you are a showkiller, i don dey chop popcorn since the inception of this thread.
I know enough c++ to have attempted to solved it in the first place, but i prefer to just watch and laugh at all the drama going on here.
Re: C Programming Challenge To Be Solved Under 10 Minutes by keneslow: 11:24pm On Nov 01, 2011
Una don finish pesin homework for am :-) , This is a common lower level programming language assignment; Intro to C abi na wetin dem dey call am. I did this particular one for a few people for 50 bucks each. Towers of Hanoi is another popular one too for recursion.

@ dell_net - C sure is easier but C++ has a lot more functionality; Objects, Templates, STL, BGL etc.

(1) (Reply)

What The Different Between ENUM And ARRAY In Java / Contest[closed] : Program A Function to find if a phrase. is in a string / It's easy, learn a new tech skill.

(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. 22
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.