Jump to content

Works on Windows, not on Mac

- - - - -

  • Please log in to reply
5 replies to this topic

#1
CharlieM

CharlieM

    Newbie

  • Members
  • Pip
  • 3 posts
Hello

I am doing some C programming at college.
This program should check if a word remains the same spelled backwards. (palindrome)
I am programming on a Mac, it compiles but always says it's not a palindrome. I asked a friend to compile on his Windows machine, it works for him.
It does work on my mac when i give a word of 1 character.

How is this possible?


Thanks in advance

Charlie

#include <stdio.h>

#include <string.h>


int isPalindroom(char *woord);


int main(int argc, char **argv) {

	char woord[100];

	printf("Give a word:" );

	scanf("%s", woord);

	if (isPalindroom(woord)) {

		printf("Palindrome!\n");

	} else {

		printf("Not a palindrome\n");

	}

	return 0;

}


int isPalindroom(char *woord) {

//Vul aan


	int i;

	int lengte = strlen(woord);


	if(lengte==0 || lengte==1) {

		return 1;

	} else {

		if(woord[0]==woord[lengte-1]) {

				

		/*for(i=0; i<lengte; i++) {

				woord[i]= woord[i+1];

				printf("%s\n", woord);

			}

			woord[lengte-2]= '\0';*/

			

			woord[lengte-1]= '\0';

			woord = woord + 1;

			

			isPalindroom(woord);

		} else {

			return 0;

		}

	}

}


#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas

CharlieM said:

woord = woord + 1;

This won't work, since you've declared woord as a statically allocated array. If you want to do pointer arithmetic on woord, it would be much better to declare it as a char*.

Edited by gregwarner, 09 June 2011 - 08:57 AM.

Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
It's a common mistake for beginning programmers to think of array variables as the exact same as pointers. True, they are similar, but they are not always interchangeable.

When you declare an array statically on the stack as you have in your source above, the identifier for that array becomes an unmodifiable lvalue. In other words, you may use it as a pointer on the right side of the '=' operator, but not the left side.

For an extremely thorough teaching on pointers vs. arrays, I suggest you read this article:
A Tutorial on Pointers and Arrays in C
About a quarter of the way down the page is where it starts talking about the stuff that's relevant to what you're doing, but I highly recommend you read it in its entirety to gain a deeper understanding of pointers and arrays.

Hope this helps.

EDIT: If a mod wants to merge my post with my previous post please, wasn't thinking and accidentally double posted.
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#4
CharlieM

CharlieM

    Newbie

  • Members
  • Pip
  • 3 posts
Okay, thanks for your help! :) I changed it like this:
char buffer[100];

	int lengte;

	printf("Give a word:" );

	scanf("%s", buffer);

	lengte = strlen(buffer);

	char* woord = malloc(sizeof(char)*lengte);	

	strcpy(woord,buffer);


#5
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
You really don't even need to perform the string copy. Simply point woord to the address of buffer.

char buffer[100];

char* woord = buffer;


Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#6
CharlieM

CharlieM

    Newbie

  • Members
  • Pip
  • 3 posts
I think I understand now. Your explanation was really helpful, I'll also read the tutorial




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users