Question:
C++ programming, use of pointers?
Stan
2009-07-25 14:17:57 UTC
Write a program that will determine whether a word is a palindrome. Place a word entered by the program user into an array and use pointers to compare the character elements for the palindrome. A palindrome is a word that is spelled the same way both forward and backward

no idea how to being please help
Five answers:
Tim M
2009-07-25 14:37:00 UTC
Do you understand pointers? If not, I recommend you read your textbook because it will be the best explanation. For a brief summary, check out http://www.youtube.com/watch?v=6pmWojisM_E . It's pretty descriptive, I think.



Set up your array like you normally would and fill it with the user's input. Then set a pointer to point to the first character, and another to point to the last character. Check if the two are equal. Increment the first pointer and decrement the second. Check again. Repeat until you've crossed the middle.
Mark aka jack573
2009-07-29 09:07:29 UTC
You have done a good effort, but there are a few things in the code to be changed.



I may be wrong, and hopefully someone might correct me.



// This may be the wrong way to pass it to the method (it has been a while since I had to do something like this). If it is, you should know how to do it.

bool is_palindrome(char *word, int size);



void main()

{

char q;

int n;

char *word[n];

cout<<"How many letters are in the word?"<
cin>>n;

cout<<"Enter the letters of the word, pressing enter after each"<
for (int i=0;i
{

cin>>*word[i];

}

if (is_palindrome(word, n)) // You may need to change how you pass the parameter 'word'. You should know yourself, or do some trial and error.

{

cout<<"The word you entered is a palindrome; a word that is spelled the same way both forward and backward"<
}

else

{

cout<<"The word you entered is not a palindrome"<
}

}



// This may be the wrong way to pass it to the method (it has been a while since I had to do something like this). If it is, you should know how to do it.

bool is_palindrome(char *word, int size)

{

// The following has been copied from the other answer. The other answer contained some errors though. The errors have been fixed. The &word[] may be wrong.

char *pEnd = &Word[size - 1];

char *pStart = &word [0]; // could also use pStart = word;



// now, find end of the word to start search

while ((pEnd != '\0') && (pEnd > pStart))

{

pEnd--;

}



if (pEnd != pStart)

{

pEnd--; // move to last char in word

}

else // This has been added.

{

return true;

}



// This has been changed.

while (pStart < pEnd)

{

// This has been changed.

if (*pStart != *pEnd) {

return false;

}

pStart++;

pEnd--;

}

// This has been changed to return a bool.

return true;

}
Peter H
2009-07-25 14:34:32 UTC
Declare an array of char TextArray for the text to be investigated for its palindromic properties. Determine the length of the array and assign that value to the integer n. Declare a char pointer p to point to TextArray.

Now p[0] is the first character of the array, which needs to be the same as p[n-1], the last character of the array. If they are the same, then test whether the second character p[1] and the second last character p[n-2] are the same. And so on.
2009-07-25 14:29:14 UTC
Assuming you know how to put the word into an array of characters (char word[20] using cin, then);



char *pEnd = &word[19];

char *pStart = &word [0]; // could also use pStart = word;



// now, find end of the word to start search

while (pEnd != '\0') && pEnd> pStart)

pEnd--;



if (pEnd != pStart)

{

pEnd--; // move to last char in word

}



bool palindrome = true;

while (palindrome && (pStart != pEnd))

{

palindrome = *pStart == *pEnd; // still palindrome if last char is same as first

pStart ++;

pEnd--;

}



At this point if palindrome is true, then characters from end to left, are same as characters from start to right and thus you have a palindrome.
michele
2016-05-23 13:12:21 UTC
hi, pointer is a powerful feature in C and C++ . Its possibilities huge. pointers allow us to change data in a memory address directly. when we use variables we use an indirect addressing. means we just make a request to the Operating System. by using pointers we directly access the memory and we can modify it. there is no mediation of OS. Possibilities are at run time we can modify data kept in memory locations directly. means we can modify an another programs instructions that are loaded in to memory. by using variables this is not possible. because an OS level protection exists there. here is some pointer examples and info A pointer is also a data type. (what is a data type, a macro that allocates specific amount of memory ) A pointer can be of any data type. The size of pointer is always 4 bytes in a 32 bit system or 32 bit compiler ( whatever be the type , int, char,double ,long .. whatever) Because a memory address is 32 bit long. and a pointer keeps the memory address so it requires 4 byte to keep it. & 'ampersand' operator will give the starting address of a data type. Initially a pointer, when assigned, pointer to the first byte of the sequence of bytes. means if you declare a variable double x ; 8 bytes will be allocated. say from 0xFF00AA01 to 0xFF00AA08 and if you are assigning this variables address to a pointer double * Xptr = &x ; Xptr will be pointer pointing to 0xFF00AA01. remember you have to assign address of a data type to a pointer declared with same data type identifier. ex. you are not supposed to assign address of x to int *Xptr. it should be double *Xptr. this is because in-order to perform arithmetic operation this is mandatory. i will explain why. int num = 0 ; // consider address of num is 0xFFFF00A1 int * numptr = & num; // now numptr contains 0xFFFF00A1 you can do numptr++; what are you expecting now for the value of numptr?? if you think value of numptr is 0xFFFF00A2 . it is wrong value of numptr will be 0xFFFF0A3. why? it is because the size of int type is 2 bytes. since numptr is a int type pointer, numptr++ mean get next int location. ie numptr++ means numptr = numptr + 2. in case of a char data type it is ptr = ptr +1 for double ptr =ptr+8 for float & long ptr = ptr +4 understood?? now comes arrays.. array of pointers and pointer to array array of pointers means int *ptr [10] ; this will be keeping 10 pointers of int data type variables. pointer to array means a pointer pointing to an array. for arrays the name of array keeps the starting address of the memory sequence means , int num[10] ; // consider starting address is 0xFFFF00A1 so num will be having 0xFFFF00A1 . note that i am not talking about num[0] .. num[n]. so you can simply assign intr *numptr = num; instead of int * numptr = # this is a short introduction to pointers. ok. if you need any more help let us know


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...