adding a space to a string

This is a discussion on adding a space to a string within the C Programming forums, part of the General Programming Boards category; how do i add a space to the end of a string? this function takes the users first, middle, and ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    21

    adding a space to a string

    how do i add a space to the end of a string?


    this function takes the users first, middle, and last name. then it puts them all together in the string result. the output shows no spaces between the first, middle and last names. how do i add teh space.

    i've tried doing something like...

    result[ i ] = result[ i + ' ' ] ;

    but it has no effect. when i put that inside the for loop, it displayed these weird characters.

    Code:
    void concat( char result[], char first[], char middle[], char last[] )
    {
    	int i, j, k ;							/* Loop counters */
    
    	for ( i = 0 ; first[ i ] != NULL ; i++ )		/* Copy first to the */
    		result[ i ] = first[ i ] ;				/* string result */
    
    	for ( j = 0 ; middle[ j ] != NULL ; j++ )		/* Copy middle to the end of string */
    		result[ i + j ] = middle[ j ] ;				/* result from where we left off. */
    
    	for ( k = 0 ; last[ k ] != NULL ; k++ )			/* Copy last to the end of string */
    		result[ i + j + k ] = last[ k ] ;			/* result from where we left off. */
    
    	result[ i + j + k ] = '\0' ;					/* Add the null byte to the end of the string */
    }
    Watshamacalit

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Your biggest problem is fooling around with multiple indices when you could more easily reuse one variable for that purpose:
    Code:
    void concat ( char result[], char first[], char middle[], char last[] )
    {
      int i, j = 0;
    
      for ( i = 0; first[i] != '\0'; i++ )
        result[j++] = first[i];
    
      result[j++] = ' ';
    
      for ( i = 0; middle[i] != '\0'; i++ )
        result[j++] = middle[i];
    
      result[j++] = ' ';
    
      for ( i = 0; last[i] != '\0'; i++ )
        result[j++] = last[i];
    
      result[j++] = '\0';
    }
    >first[ i ] != NULL
    This is generally a bad idea, the NULL macro may not be the same as the nul character ('\0') so you really shouldn't use them interchangeably.

    This function could be simplified greatly by using the standard library as well:
    Code:
    void concat ( char result[], char first[], char middle[], char last[] )
    {
      sprintf ( result, "%s %s %s", first, middle, last );
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  5. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM

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