Seperate string into 2 based on last + sign in the string

This is a discussion on Seperate string into 2 based on last + sign in the string within the C Programming forums, part of the General Programming Boards category; 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 ...

  1. #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.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    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 .

  3. #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.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  5. #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?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  7. #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.

  8. #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.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    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...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  10. #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?

  11. #11
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    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.

  12. #12
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    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

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    Thank you. It works

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread


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