How the fuck do you write a palindrome generator function in C that takes an argument in the form of a char array.
were not going to do your homework, anon
>>58798168
I bet this is a homework assignment.
However, what is a palindrome generator?
Something that makes abcba from abc?
Carefully
Not /g/.
Go to the homework board.... kid.
>>>/hm/
>>58798234
that wasnt homework at all anon!
>>58798210
i can't figure how how the fuck do it. This is my code so far:void test(char *s){
char palindrome[sizeof(s)*2];
for(int i = 0; i < sizeof(s)*2; i++){
if(i < sizeof(s)){
palindrome[i] = s[i];
}else{
palindrome[i] = s[palindrome-i];
}
}
}
>>58798168
C is a palindrome, so you answered you're own question.
>>58798302
Anon, that sizeof(s) will return 4 (or 8 on x64) since it's the size of the pointer.
>>58798302
1. figure out the input string size
2. depending on odd or even, allocate a char* with twice or one less than twice the size; take the string terminator into account
3. copy the string
4. reverse-copy the string into the remaining fields, leaving out the first char if odd
5. set the null terminator
>>58798302
the variable palindrome isn't an integer. Why are you trying to use it as an index to s in the else statement?
>>58798384
>set the null terminator
?
>>58798357
char is guaranteed to be 1 anyhow
>>58798849
which is completely irrelevant
>>58798845
set the \0 thing at the actual end of the string to tell C the string ends there
>>58798168#include<stdio.h>
main(int argc, char** argv){
int i;
for(i = 0;i<(sizeof(argv[1])-1);++i){
putchar(argv[1][i]);
}
for(i = (sizeof(argv[1]) -2);i>=0;--i){
putchar(argv[1][i]);
}
}
>Can anyone optimize this to use only one loop, for while or anything. Another counter is accepted since one less loop >> one more counter.
>>58800480if(argc!=2)
return;
Sry forgot to write this line.
>>58800480
Its wrong.
>>58800480for(i = 0; i<strlen(argv[1]);++i)
.
.
for(i = strlen(argv[1]);i>=0;--i)
Corrected
We have a thread going called /sqt/ for stupid questions, freshman
>>58798302
Use the Heap, nigger. Tomorrow when I input a 2mb string, what will you do?
>>58798849
char is. char* isn't.
>>58798168
You're a dumb fuck if you haven't figured out which site your teach is pulling these problems from.
>>58798168
just make two slicing functions, one for even numbers and one for odd, and a comparison you reetard
>>58798168
> Palindrome generator
Well, you get the input and you print it and then you print the reverse...