[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

How the fuck do you write a palindrome generator function in

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 25
Thread images: 3

File: 1412843268742.jpg (55KB, 957x621px) Image search: [Google]
1412843268742.jpg
55KB, 957x621px
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/
>>
File: 1477357530156.gif (782KB, 303x346px) Image search: [Google]
1477357530156.gif
782KB, 303x346px
>>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.
>>
>>58800480
if(argc!=2)
return;

Sry forgot to write this line.
>>
>>58800480
Its wrong.
>>
>>58800480
for(i = 0; i<strlen(argv[1]);++i)
.
.
for(i = strlen(argv[1]);i>=0;--i)



Corrected
>>
File: 52692134625.png (29KB, 594x838px) Image search: [Google]
52692134625.png
29KB, 594x838px
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...
Thread posts: 25
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.