Thread: Joining array elements and save in variable.

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Joining array elements and save in variable.

    Hi,

    Is it possible to join together char array elements, like '+' them together and store them in a char variable ? How ?

    thnx

  2. #2
    Unregistered
    Guest
    You just described a char array
    But if you mean take an array and scrunch the characters into a single char variable then it's not possible. Unlike int where 1, 12, and 123 are all valid ints and can be put together to make another int, char can only have 'a'; not 'ab' or 'abc'. That's precisely why we have arrays, to group data like that when we want and still take it out as it was without too much trouble.

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    i want to do it because i need to do the palindrome exercise and i have to use recursive techniques. Can you show me the pseudocode for the function for me to study?

    thnx

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Use strcat to combine strings, but make sure that the destination string is large enough to hold the combined string:
    Code:
    #include <stdio.h> 
    #include <string.h>
    
    
    int main()
    {
       char str1[20] = "Hello";
       char str2[] = "World";
    
       //white space between the words hello and world
       strcat(str1," ");
       strcat(str1,str2);
       printf("Combined string: %s", str1);
    
       return 0;
    }

  5. #5
    Unregistered
    Guest
    There are two exercises for palindromes, the first is usually a school program so I'll assume that's the one you want. It reads in a string and determines if it's a palindrome.
    Code:
    int isPalindrome(char *string) 
        IF the length of string is 0 or 1 
            RETURN true 
        ELSE IF the first and last character of string are not the same 
            RETURN false 
        ELSE 
            MODIFY the string by removing its first and last characters 
            ADD '\0' to the end of the modified string
            RETURN isPalindrome(string)

  6. #6
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    To remove the first and last element in the array, do i just pass &array[ 1 ] as the array to the recursive function. But how do i remove the last element in the array?

    thnx

  7. #7
    Unregistered
    Guest
    To clear the last element
    array[size-1] = '\0';

    To clear the first
    array[0] = '\0';

    Or you can create a new array and place the contents of your current one minus the first and last elements into that:
    Code:
    int i = 1;
    while(i < size-2){ /*to account for the \0 and last element*/
      array2[i-1] = array[i];
      ++i;
    }

  8. #8
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    thnx

    i just don't know why i also make problems more complicated than they really are. Well, after all, i'm still new to C, excuse lol.

    thnx

  9. #9
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Palindrome program:
    Code:
    #include <stdio.h> 
    #include <string.h>
    
    int main()
    {
    	char word[20];
    	char palindrome[20];
    
    	printf("Enter word: ");
    	fgets(word,20,stdin);
    	word[ strlen(word) -1 ] = '\0';
    
    	char *begin = word;
    	char *end = word + strlen(word) - 1;
    
    	int count = 0;
    	while(*end == *begin)
    	{
    		palindrome[count] = *begin;
    		palindrome[count + (end - begin)] = *end;
    		count ++;
    		begin ++;
    		end --;
    	}
    	palindrome[count] = *begin;
    	palindrome[strlen(word)] = '\0';
    	if(strcmp(palindrome,word) == 0)
    		printf("Palindrome!");
    	else
    		printf("Not a palindrome!");
    
       return 0;
    }

  10. #10
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    this exercise i'm up to haven't yet taught me how to use pointers, so i'm not suppose to use them.

  11. #11
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    HI,

    I just wrote the code but no matter what string i pass it returns false. How come ? thnx.
    Code:
    /* Palindrome */
    
    #include <stdio.h>
    
    int testPalindrome( char array[] );
    
    int main()
    {
       char string[ 6 ] = "ellle";
    
       if ( testPalindrome( string ) == 1 )
          printf( "String is a palindrome.\n " );
       else if ( testPalindrome( string ) == 0 )
          printf( "String is not a palindrome.\n" );
    
       getch();
    
       return 0;
    }
    
    int testPalindrome( char array[] )
    {
       int length = 0;
       int i = 0;
    
       i = 0;
    
       while ( array[ i ] != '\0' ) {
          ++length;
          i++;
       }
    
       if ( length - 1 == 0 || length - 1 == 1 )
          return 1;
       else if ( array[ 0 ] != array[ length - 2 ] )
          return 0;
       else {
          array[ 0 ] = '\0';
          array[ length - 2 ] = '\0';
          for ( i = 0; i < 6; i++ )
             printf( "%c", array[ i ] );
    
          return testPalindrome( array );
       }
    }
    Last edited by Nutshell; 01-10-2002 at 11:37 PM.

  12. #12
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Quick question.......why does everything you have posted involve recursion?

    Is it really that important that you use it?

  13. #13
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    hI,

    i'vce already figurered the solution, it's in the other thread. I need to do recursion coz some exercises in my book is asking me to do recursion and btw i really need to do some recursion, i am not very familiar with it thats why.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array of strings and memory allocation
    By maluyk in forum C Programming
    Replies: 7
    Last Post: 01-26-2009, 11:52 AM
  2. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 04:19 AM
  3. pointers, arrays.
    By Raeliean in forum Game Programming
    Replies: 5
    Last Post: 07-18-2005, 02:43 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. two dimensional dynamic array?
    By ichijoji in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 04:27 PM