Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-4041-

Woohoo simple school assignments

Name: Anonymous 2010-05-03 18:07

Hey there /prog/, you guys seem less than retarded, so I'm asking you for your help. I need to do some palindromes, just single words, but I'm retarded.

Does anyone know why my Sepples codes doesn't work? 'x' isn't set to the right value no matter the input, which throws off the palindrome checking function, which sucks cocks.

Here's the code. Note, I cannot use any str functions.


//Project 14 - Palindromes
#include <iostream>
using namespace std;

int main()
{
    char str[100];
    int x = 0; //Will be the string length
    cout << "Enter a word to see if it's a palindrome: ";
    cin >> str;
    for(int i = 0; i < 100; i++) {
        if (str[i] == '\0') {
            x = i - 1;
        }
    }
    cout << "X = " << x << endl;
    for(int i = 0; i <= x; i++){
        cout << str[i] << "   " << str[x-i] << endl;
        if (str[i] == str[x-i]) {
            continue;
        }
        else {
            cout << "That word is not a palindrome. " << endl;
            return 0;
        }
    }
    cout << "That word is a palindrome!" << endl;
    return 0;
}


Where am I fucking up?

Thanks, I love you /prog/

Name: Anonymous 2010-05-03 18:09

Try the str function.

Name: Anonymous 2010-05-03 18:12

//Project 14

How the fuck did you survive the 13 previous assignments if you need help with this?

You're wasting your time with education.
McDonald's is always looking for new talent.

Name: Anonymous 2010-05-03 18:15

Here's the code. Note, I cannot use any str functions.
My first response would have been to write those functions.

Name: Anonymous 2010-05-03 18:21

Hope this helps:

s=raw_input("Enter a word to see if it's a palindrome: ");i=len(s)/2;print "That word is "+["not a palindrome. ","a palindrome!"][s[:i]==s[::-1][:i]]

Name: Anonymous 2010-05-03 18:23

>>5

Oddly enough, it would help, but I'm sure the one thing fucking me up is the calculation of the length of the string, as I have to do that manually.

Name: Anonymous 2010-05-03 18:24


'-._                  ___.....___
    `.__           ,-'        ,-.`-,
        `''-------'          ( p )  `._   
                              `-'      (  HAVE YOU READ
                                        \   YOUR SICP
                                        \     TODAY?
                               .---..,--'
   ................._          |--...--,
                     `-.._         _.-'
                          `'-----''

Name: Anonymous 2010-05-03 18:26

Also, word == word[::-1]

Name: Anonymous 2010-05-03 18:26

>>6
If you can't implement strlen on your own you don't deserve a computer. Go outside and play with your friends.

Name: Anonymous 2010-05-03 18:28

>>8
Yes but the version written in >>5 is more ZOMG OPTIMIZED.

Also, Brainfuck implementation coming up soon.

Name: Anonymous 2010-05-03 18:31

I'm a C-fag, but why don't you #include <Strings> (or String or w/e it's called) and use the String class instead of using a char array?

In any case, you do not need to subtract 1 from i when assigning to x.

Name: Anonymous 2010-05-03 18:40

>>1
[code] tags? Indentation? Comments? Are you sure that's homework? Looks pretty... competent.
>>11
C-fag
back to /b/, please

Name: Anonymous 2010-05-03 18:42

>>12

I'm really fucking anal about my code structure.

Name: Anonymous 2010-05-03 18:43

>>11
[b]Back to /b/, please[/b

Name: Anonymous 2010-05-03 18:53

format ELF64

extrn printf
extrn gets

section '.text' executable
public main
main:
    xor eax, eax
    lea edi, [msg]
    call printf

    xor eax, eax
    lea edi, [_str]
    call gets

    xor edi, edi
    lea esi, [_str]
@@:    lodsb
    inc edi
    test al, al
    jnz @b

    dec edi

    lea esi, [_str]
@@:    lodsb
    dec edi
    test al, al
    jz .palin
    cmp al, [_str + edi]
    je @b

.nonpalin:
    xor eax, eax
    lea edi, [nonpalin]
    jmp printf
.palin:
    xor eax, eax
    lea edi, [palin]
    jmp printf

section '.bss' writeable
_str:    times 100 db ' '

section '.data'
msg:    db "Enter a word to see if it's a palindrome: ", 0
palin:    db "It's a palindrome!", 10, 0
nonpalin: db "It's not a palindrome ;_;", 10, 0

Name: Anonymous 2010-05-03 19:10

>>15
What assembler is this with the @@: / @b shit?

Name: Anonymous 2010-05-03 19:16

I'll have you know I am more than retarded.

Name: Anonymous 2010-05-03 19:25

>>16
FASM

Name: Anonymous 2010-05-03 19:31

>>1
You need a break; in your string length counting loop after finding the terminator.
Also, you can use this more compact version instead.

while(str[x] != '\0' && x++ < 100);
x--;

Name: Anonymous 2010-05-03 19:33

>>19
Please use ++x and --x instead, they are faster.

Name: Anonymous 2010-05-03 20:21

>>20
What about x += 1 and x -= 1;?

Name: Anonymous 2010-05-03 21:35

inline void subtract(int *value, int subtract) { *val = *val - subtraction; }
int *pX = x;
subtract(pX, 1);

Name: Anonymous 2010-05-03 21:49

>>21
++x = 3 characters
x += 1 = 6 characters
Go figure.

Name: Anonymous 2010-05-03 21:49

>>19

Thanks, don't know how the fuck I missed that.

This is why I love you, and the rest of /prog/

Name: Anonymous 2010-05-03 22:53

>>23
Computation-wise how do they compare, I mean?

Name: Anonymous 2010-05-03 23:00

>>25
No difference on a post-1980 compiler.

Name: Anonymous 2010-05-04 12:27

bool isPalindrome(char *s)
{ char *e = s;
  for(;*++e;);
  for(--e; e - s > 0;) if(*s++ != *e--) return false;
  return true; }

Name: Anonymous 2010-05-04 12:41

<>;
my $str = chomp;

$str =~ s/\W//g;
my $halflen = int( 0.5 * length $str );
die "not a palindrome you stupid fuck" unless (substr($str, 0, $halflen) eq substr (reverse ($str), 0, $halflen));

Name: Anonymous 2010-05-04 13:04

>>28
<>;
Shameful!  Lern2perl, foul newb!

Name: Anonymous 2010-05-04 14:16

>>23
Hello, Void.

Screen real estate shouldn't be an issue here, since if you have something else on the line with the incrementation, you're doing something wrong anyway.

Name: Anonymous 2010-05-04 18:49

#!/usr/bin/perl -p
$_="That line is "."not "x!/^(|(.)(?1)\2|.)$/."a palindrome.\n";

Name: Anonymous 2010-05-04 20:21

>>31
Terrible!

Name: Anonymous 2010-05-04 23:16

>>30
doing x += 1 instead of ++x is like doing do { something(); } while(!!!!!!!!!!!0); instead of for(;;) something();.

Name: Anonymous 2010-05-04 23:27

<?php
isset($_GET['s']) ? $s = $_GET['s'] : die();
$l = strlen($s);
$a = array(substr($s, 0, $l/2 + ($l % 2 ? 1 : 0)), substr($s, $l/2));
echo $s . ($a[0] === strrev($a[1]) && $l > 1 ? ' is ' : ' is not ') . ' a palindrome.';

Name: Anonymous 2010-05-04 23:47

isPalindrome = ap (==) reverse

Name: Anonymous 2010-05-05 6:54


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int ispalim(char *s) {
  char *p,*sr,*pr;
  int r,n = strlen(s);
  sr = malloc(n+1);
  pr = sr + n;
  *pr-- = 0;
  for(p=s;*p;p++,pr--)
    *pr = *p;
  r = (strcmp(s,sr)==0);
  free(sr);
  return r;
}

int main(int argc, char **argv) {
  printf("is palim: %s\n",ispalim(argv[1])?"true":"false");
}

Name: Anonymous 2010-05-05 8:20

Name: Anonymous 2010-05-05 9:37

>>36
You only need to iterate halfway, and why are you allocating memory?

Name: Anonymous 2013-09-01 13:54


I was in Japan, but it didn't really seem like Japan because there were too many white people. The people I interacted with in the dream were all caucasian at least. The Asians were just the background people, like extras in a movie. Anyways I was walking through some sort of mall. The stores were interconnected so you walk through a door and you're in the next store. I seemed to have a set destination in mind, since I wasn't browsing any of the stores and was weaving through them all. I accidentally bump into a Japanese lady and mutter something and rush off, realizing I'm in Japan and don't actually know how to apologize. So, naturally I ask twitter.

Name: Anonymous 2013-09-01 15:25


Vodka makes me black out. I may even be blacked out whenever I read and write 4chan posts.  Am I awake? Obviously Vlad the Impaler does not want it Putin the bum of a young boy. Hence the laws against homosexuality.

Don't change these.
Name: Email:
Entire Thread Thread List