Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using pointers

Status
Not open for further replies.

dgboult

Programmer
Apr 29, 2001
7
US
Hello,
I wrote this program to see if a message is a Palindrome or not. Like "He lived as a devil, eh?". My question is how can I use pointers to keep track of the current position in the array?

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

/****************************************/
/* This program reads in a message then */
/* prints if it is a Palindrome or not. */
/****************************************/

int main(void)
{

char str[100];
int a,c,j;

/*************************/
/* Reads in the message. */
/*************************/

printf(&quot;Enter a message: &quot;);
for(a = 0; a < 100; a++) {
str[a] = getchar();
if(str[a]=='\n'){
str[a] = '\0';
break;
}
}

/*****************************************************************/
/* Converts all the letters to capitals unless they already are, */
/* then gets rid of the spaces and punctuation marks. */
/*****************************************************************/

for(a=0; a<strlen(str); a++)
if(str[a]>=97 && str[a]<=122)
str[a]-=32;

for(j=0,a=0; a<strlen(str);a++)
{
if(str[a]>=65 && str[a]<=90)
{
str[j]=str[a];
j++;
}
}
str[j]='\0';

/************************************************/
/* Calculates and prints if it is a Palindrome. */
/************************************************/

c = strlen(str)-1;

for(a=0; a < strlen(str); a++, c--) {
if(str[c] != str[a]){
printf(&quot;Not a Palindrome&quot;);
return 0;
}
}
printf(&quot;Palindrome&quot;);

return 0;
}

Thanks!!

 
You have to have 2 pointers, one that points to the beginning char, the other to the last one.

...
char palWord[30];
char *f = palWord,*r = palWord + strlen(palWord) - 1;
...

Then you put it in a loop, increasing f {f ++}, decreasing r {r --}; and compare, if (*f != *r) printf(&quot;It's not a polindrome!&quot;); Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
Hi!!
I'll tell u a simple way by which u'll never have troubles with pointers.
See the name of array without its subscript reference is nothing but base address of that array. So whenever u think of pointers, think it this way.....
Instead of writing str when str is an array,
I can write it as *(str+i) if str is a pointer....
Apart from some subtle differences u can always be at home with pointers if u r comfortable with arrays.

Regards,
SwapSawe.
 
Here's a version that tests for palindromes using pointers:

#include <ctype.h>
#include <string.h>

/******************************************************
* Returns 1 if is a palindrome, 0 otherwise.
* should be a NUL-terminated string.
******************************************************/
int is_palindrome(const char *s)
{
const char * const start=s; /* Bookmark the start of the string */
const char *end=strchr(s,'\0')-1; /* Get a pointer to the end */

/* Palindromes must be at least three letters */
if (start==end || start==end-1) {
return 0;
}

/* Loop through the string, ignoring and adjusting for
* and non-alpha characters
*/
while (start!=end
&& (toupper((unsigned char)*s)==toupper((unsigned char)*end)
|| !isalpha((unsigned char)*s)
|| !isalpha((unsigned char)*end))) {
/* Does either pointer point to a non-alpha character? */
if (!isalpha((unsigned char)*s)
|| !isalpha((unsigned char)*end)) {
/* Yes, find out which one and skip it */
if (!isalpha((unsigned char)*s)) {
++s;
} else {
--end;
}
} else {
/* No, just move to the next character */
++s,--end;
}
}
return end==start;
}

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top