C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 04-18-2009, 03:00 AM   #1
Registered User
 
Join Date: Apr 2009
Posts: 13
Smile Seperate string into 2

Hi,

If I have this string 'Hello+Julie+I-was-here', how do I get this expecting result with C programming?

'Hello+Julie'

'I-was-here'

Another example,

String: 'I+am+really+tired+mememe-is-now-at-home'

Result:

'I+am+really+tired'
'mememe-is-now-at-home'

As noticed, I will want to break up my string into 2. Based on the last + sign from the back in the string, I will separate the string into 2 separate strings. How am I suppose to do this?

Last edited by JuzMe; 04-18-2009 at 03:49 AM.
JuzMe is offline   Reply With Quote
Old 04-18-2009, 03:50 AM   #2
Registered User
 
Join Date: Oct 2008
Posts: 452
Erm... What about looping through the string, mark the last + you found, replace it by a null character, and add one to get the second string..?
You could copy the string, which you have to if it's a constant string, but if not it's too much overhead.
Although, 99% of the cases the string should really be constant in this case .
EVOEx is offline   Reply With Quote
Old 04-18-2009, 03:51 AM   #3
Registered User
 
Join Date: Apr 2009
Posts: 13
Quote:
Originally Posted by EVOEx View Post
Erm... What about looping through the string, mark the last + you found, replace it by a null character, and add one to get the second string..?
You could copy the string, which you have to if it's a constant string, but if not it's too much overhead.
Although, 99% of the cases the string should really be constant in this case .
Sorry but how do I actually code that? I am really new to this C programming and so I am not so sure about it.
JuzMe is offline   Reply With Quote
Old 04-18-2009, 04:03 AM   #4
Registered User
 
Join Date: Sep 2006
Posts: 2,512
Before you can learn any trick riding skills, you have learn how to actually ride the horse, a little.

This requires using a string (a char array[]), and working with an index with that array - not the only way but probably the simplest.

Learn about using a char array and an index, and how to print a string, and a newline. Then your answer will be made clear to you, and you will have learned something valuable in the process.
Adak is offline   Reply With Quote
Old 04-18-2009, 04:52 AM   #5
Registered User
 
Join Date: Apr 2009
Posts: 13
Quote:
Originally Posted by Adak View Post
Before you can learn any trick riding skills, you have learn how to actually ride the horse, a little.

This requires using a string (a char array[]), and working with an index with that array - not the only way but probably the simplest.

Learn about using a char array and an index, and how to print a string, and a newline. Then your answer will be made clear to you, and you will have learned something valuable in the process.
Ok, do you have any reference that I can refer to?
JuzMe is offline   Reply With Quote
Old 04-18-2009, 05:29 AM   #6
Registered User
 
Join Date: Sep 2006
Posts: 2,512
A good place to start is Ivor Horton's book - "Beginning C".

Covers all the basics, in a down to earth style. Also, has several examples of great problem solving, using the basics discussed in the previous chapters.

K&R "The C Programming Language" is excellent, but not for beginners.

There are many C tutorials on-line, but you'll have to Google them up.
Adak is offline   Reply With Quote
Old 04-18-2009, 05:31 AM   #7
Registered User
 
Join Date: Apr 2009
Posts: 13
Actually, it is not say I do not want to learn. But I am stuck and rushing to meet the dateline so I do not have time to look for these books.... Is ok.... I will look at other places.
JuzMe is offline   Reply With Quote
Old 04-18-2009, 06:02 AM   #8
Registered User
 
Join Date: Apr 2009
Posts: 13
I am really sorry..... I am really desperate for help... Can anyone guide me what I should really do? I don't even know how to begin.
JuzMe is offline   Reply With Quote
Old 04-18-2009, 06:22 AM   #9
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
use strrchr to find the last '+' character and then replace it with 0 (like strtok does) or strncpy first part of the string to the side...
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 04-18-2009, 07:02 AM   #10
Registered User
 
Join Date: Apr 2009
Posts: 13
Quote:
Originally Posted by vart View Post
use strrchr to find the last '+' character and then replace it with 0 (like strtok does) or strncpy first part of the string to the side...
Hi Vart,

Thanks for your help.

But I need your help further.

For this string:

String: 'I+am+really+tired+mememe-is-now-at-home'

With strrchr, I was able to get 'mememe-is-now-at-home'.

But the problem is how do I get I+am+really+tired?

Does anyone knows about it?
JuzMe is offline   Reply With Quote
Old 04-18-2009, 07:12 AM   #11
DESTINY
 
BEN10's Avatar
 
Join Date: Jul 2008
Location: in front of my computer
Posts: 656
Quote:
Originally Posted by JuzMe View Post
Hi Vart,

Thanks for your help.

But I need your help further.

For this string:

String: 'I+am+really+tired+mememe-is-now-at-home'

With strrchr, I was able to get 'mememe-is-now-at-home'.

But the problem is how do I get I+am+really+tired?

Does anyone knows about it?
show the code u have made till now.
BEN10 is offline   Reply With Quote
Old 04-18-2009, 07:19 AM   #12
Making mistakes
 
Join Date: Dec 2008
Posts: 347
that's easy. Like vart said:

Code:
char *end = strrchr(string, '+');            /* Get the last occurrence of '+' */
end[0] = '\0';                               /* Replace it with a nul byte */
end++;                                       /* Skip it */
string is I+am+really+tired, and end is mememe-is-now-at-home
Brafil is offline   Reply With Quote
Old 04-20-2009, 06:44 AM   #13
Registered User
 
Join Date: Apr 2009
Posts: 13
Thank you. It works
JuzMe is offline   Reply With Quote
Reply

Tags
seperate, string, word

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
find a string based on the location of another string rlilley C Programming 3 02-19-2009 12:29 PM
OOP Question DB Access Wrapper Classes digioz C# Programming 2 09-07-2008 04:30 PM
String Class BKurosawa C++ Programming 117 08-09-2007 01:02 AM
RicBot John_ C++ Programming 8 06-13-2006 06:52 PM
can anyone see anything wrong with this code occ0708 C++ Programming 6 12-07-2004 12:47 PM


All times are GMT -6. The time now is 06:46 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22