strings and palindrome's

This is a discussion on strings and palindrome's within the C Programming forums, part of the General Programming Boards category; i'm supposed to create a function that checks if an inouted word or number is a palindrome( same forwards and ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    21

    strings and palindrome's

    i'm supposed to create a function that checks if an inouted word or number is a palindrome( same forwards and backwards i.e bob )...the only problem that i see in my program is getting the number backwards...

    my problem is in my for loop below...

    for( i = 0 ; i <= amt ; i++ )
    backwards[ i ] = input[ amt - i ] ;

    what happens is the null byte becomes the backwards[ 0 ], because it is the last element in the array input...how do i get the null byte to be the last element of the array?
    Watshamacalit

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Look up fgets() in your helpfiles. Use it to get all input as a string Find strings length. Then check if string is palindrome.Simple pointer exercise.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    14
    try this (assuming that amt is the length of the input string)

    Code:
    for( i = 0 ; i <= (amt-1) ; i++ )
    backwards[ i ] = input[ amt - (i+1) ] ;
    
    backwards[i]= '\0';

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Why not try searching for an answer.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    try this
    Code:
    #include <stdio.h> 
    
    int main(void)
    {
       char aString[] = "boibbvob";
       char buff[20];
       
       int i, j, length;   
       j=0;
       
       for(i=0; aString[i] != '\0'; i++);
       
       length = i;
       
       for(i = length-1; aString[i] > 0; i--)
       	buff[j++] = aString[i];
       
       buff[j] = '\0';
       
       printf("%s\n", aString);
       printf("%s\n", buff);
       
       j=0; 
       
       for(i=0; aString[i] != '\0'; i++)
       {
          if(buff[j++] != aString[i])
          {
          	printf("\nstrings are not palindrome");
          	return -1;
          }
       }
         	 
       printf("\nstrings are palindrome");
           
    
       return 0;
    }

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    Why not try searching for an answer.
    Why bother? Everyone shows up and does your homework for you. I'm getting to the point where I don't even bother reading homework issues. There's no point in helping people learn. No one wants to actually learn anything. They just want their work done for them, and there are countless people jumping at the chance to do all the work for them...

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Originally posted by quzah
    There's no point in helping people learn. No one wants to actually learn anything.
    Not so; it's the only reason that I come here. I've taken a lot of things from this board.... much more so than in any class room setting or from passive learning like books that the author often pens in such a way that is specific to his or her own style. The board offers a huge variety of ideas.

    If a person chooses not to understand what is going on and turns it in replies as a homework assignment, then those 10 points coming from his or her homework grade will be of great use around the time said person has to drop a course because of a 25 test average. That choice is entirely up to each individual as to what he or she chooses to learn.
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Palindrome in strings
    By JFonseka in forum C Programming
    Replies: 4
    Last Post: 08-31-2007, 07:40 AM
  2. using Stacks & Queues to compare Strings
    By eskimo083 in forum C++ Programming
    Replies: 1
    Last Post: 03-09-2003, 05:03 PM
  3. pointer and strings
    By lostpoet in forum C Programming
    Replies: 8
    Last Post: 03-08-2002, 11:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21