Thread: How to take a char string and separate it at a capital letter?

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    43

    How to take a char string and separate it at a capital letter?

    I need to write a program where a name is to be entered as a continuous string, such as "JohnDoe" and then it prints out "John Doe". The stipulation is that the first name becomes its own string and the second name is also its own string. So basically the input string gets split and assigned to string a and string b, where the second capital letter exists.

    I tried having the program convert each letter to ASCII and then starting the new string when it hit a value within the ASCII capital letters. But that was a mess that I couldnt even get to compile. That doesnt seem like the right approach.

    Logically, how would you guys approach this? We just got done talking about strlen, strcpy, strcat, etc so I'm thinking the solution may lie there, but I'm not seeing how to utilize that, or if its the easiest way.


    Thanks,
    crazychile

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    I'd probably do it something like this:
    Code:
    for each character in "JohnDoe" starting at the second letter
      if the character is a capital
        copy the rest of the string to a new array
        change the current character into a '\0' to terminate the string
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    It sounds like you are on the right path in scanning for up case letters, when you find one, keep that address and use it in strcpy. Of course, you need to decide how you will use memory here as you need somewhere to copy to. Either use some pre-allocated space that is "big enough" or allocate dynamically after you have obtained the length of the string.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    That structure makes sense. Thank you.

    So I would still be checking the character by its ASCII value to determine a capital letter, or is there an easier way?


    thanks,
    crazychile

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by crazychile View Post
    That structure makes sense. Thank you.

    So I would still be checking the character by its ASCII value to determine a capital letter, or is there an easier way?


    thanks,
    crazychile
    You don't have to look for the value, you can use the function isupper() declared in ctype.h, it takes a character as argument and return true (non zero) if it's an upper case letter.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Or check that the character is (ch >= 'A' && ch <= 'Z')

  7. #7
    Registered User
    Join Date
    Sep 2008
    Posts
    43
    Quote Originally Posted by nonoob View Post
    Or check that the character is (ch >= 'A' && ch <= 'Z')
    Yeah, I'm liking that. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my assigment
    By cloverdagreat in forum C Programming
    Replies: 16
    Last Post: 11-21-2009, 12:18 PM
  2. Hangman game and strcmp
    By crazygopedder in forum C Programming
    Replies: 12
    Last Post: 11-23-2008, 06:13 PM
  3. A Question About Unit Testing
    By Tonto in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2006, 08:22 PM
  4. String Processing Problem
    By muffin in forum C Programming
    Replies: 0
    Last Post: 08-24-2001, 10:13 AM