Hi,
Is it possible to join together char array elements, like '+' them together and store them in a char variable ? How ?
thnx
Hi,
Is it possible to join together char array elements, like '+' them together and store them in a char variable ? How ?
thnx
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.
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
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; }
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)
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
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; }
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
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; }
this exercise i'm up to haven't yet taught me how to use pointers, so i'm not suppose to use them.
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.
Quick question.......why does everything you have posted involve recursion?
Is it really that important that you use it?
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.