Thread: Help with writing a program to "split" a string

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    75

    Help with writing a program to "split" a string

    Hi all

    I have to write a program that will split a person's name by first and last...so for example one enters JoeWalsh and the output is Joe Walsh. For our purposes we can assume the first letter is capitilized and there is only one other signifying the beginning of the person's last name. I think I am pretty close...the code I have now gives me Joe alsh, when I enter in the JoeWalsh...I know I just can't figure out to get something like ' '&input[i](meaning the space and input[i] are cancatenated some how) during the if function. Any help is greatly appreciated.



    Code:
    void split(char input[])
    {
    int i;
    //int j;
    
    i=0;
    j=0;
    for (i=1;input[i] != 0;i++)
    {
    if (input[i] >= 'A' && input[i] <= 'Z')
      {
    input[i] = ' ';
    //input[j] = input[i];
    // j++;
      }
    
    }
    }
    int main()
    {
    char output[80];
        printf("Enter any string->");
    scanf("%s",output);
    split (output);
    //char ret;
    
     //ret = devowel(input);
    printf("split %s\n", output);
    return 0;
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're going to have to shift everything starting at the second capital letter over one position to make space for the index.
    Code:
    if(isupper(input[i]))
    {
      memmove(input + i + 1, input + i, strlen(input + i + 1));
      input[i] = ' ';
    }
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    75
    Hi thanks for the answer....we are not allowed to use strlen for this exercise, I could write a function I guess to give me the lingth without using it....but we probably can't use memmove either..is there another way to go about this?

  4. #4
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    you should try
    while(str[i++]!='\0') ;
    i will give you the length
    You ended that sentence with a preposition...Bastard!

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    75
    Hi Eman...you mean instead of a for loop? Ok i think I can do that but how do I get that space before the second capital letter...and for that matter how do I know make sure the first capital letter is skipped?

  6. #6
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    oh well you said you didn;t want to use strlen()
    well I think if the next letter to be found is a capital letter and tells you that it is where the space should be.
    i will do
    while(i>0) so I know for sure I won't check the first letter

    while(i>0 && str[i]<str[i-1])
    sorry i haven't tried the code but I think it just says if the ascii value of str[i] is less than 'a' to 'z' (that is the previous value)..I believe they have greater Ascii values
    then i store str[i] in a temp value..and then replace that index with a space..and then ...you get the point..I don't want to mislead you so i will shut up
    You ended that sentence with a preposition...Bastard!

  7. #7
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    you are not allowed to use other functions?
    You ended that sentence with a preposition...Bastard!

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    75
    No we can't really use any of the string library functions....I'm I had somewhat of a hard time following what you were saying about str[i] && stri[i-1] not sure how to apply that.

  9. #9
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    well instead of trying to do
    if (input[i] >= 'A' && input[i] <= 'Z')

    to check if you are the point to put a space
    just do:
    if(strName[i] <'a')

    that should work right? will try it now
    You ended that sentence with a preposition...Bastard!

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    75
    yes uppercase letters are lower ascii values then lowercase, but I'm not sure how to store in a temporary value?

  11. #11
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Where are you going to store the first & last names? First this goes into you split function as 1 string, but the result will be 2 strings. May I propose something like this.

    Code:
    struct Name {
            char *first;
            char *last;
    };
    
    void split(char *str, struct Name *name);
    This assumes that your original string will not be thrown away, or your pointers will be useless.

  12. #12
    Registered User
    Join Date
    Jan 2011
    Posts
    75
    hi....I'm not sure what a struct is how would I write the following code? Not sure how to store?

  13. #13
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    haha i just wrote this without thinking .but you can improve on it
    Code:
    #include <iostream>
    using namespace std;
    int main()
    {
    	char strName[90] ="JoeHoe" ;
    
    	char str[90] = "JoeHoe" ;
    	int i=0; 
    	
    	;
    	while(i++>=0)
    	{
    		if(strName[i] <'a')
    		{
    			break ;
    		}
    		
    
    	}
    	
    	int j=i ;
    	str[j] =' ';
    	while(strName[i]!='\0')
    	{
    		 str[++j]	= strName[i++] ;
    		
    	}
    
    	cout << str;
    	cin.get() ;
    	return 0; 
    }
    Last edited by Eman; 02-20-2011 at 06:06 PM.
    You ended that sentence with a preposition...Bastard!

  14. #14
    The Dragon Reborn
    Join Date
    Nov 2009
    Location
    Dublin, Ireland
    Posts
    629
    oops..i did it again...C forum aargh

    he can't use a char*
    he wouldn't be able to modify a literal
    You ended that sentence with a preposition...Bastard!

  15. #15
    Registered User
    Join Date
    Jan 2011
    Posts
    75
    nope I just found that out..last night actually

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM