Thread: Problem with limiting inputs of char functions

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    8

    Problem with limiting inputs of char functions

    Code:
    #include <stdio.h>
    #define MAX_INPUT_LENGTH 80
    
    int is_palindrome(char []);
    
    int main(void)
    {
    	int i = 0, indicator = 0;
    	char words[MAX_INPUT_LENGTH] = {'a'};
    
    	do
    	{
    		do
    		{
    			scanf("%c", &words[i]);
    			
    			i++;
    		} while(words[i] != '\n');
    		
    		indicator += is_palindrome(words);
    
    	} while(words[i] != '\n' && words[i-1] != '\n');
    
    	return 0;
    }
    
    int is_palindrome(char arr[])
    {
    	int max, max_temp, i;
    
    	for(i = 0; i < MAX_INPUT_LENGTH; i++)
    	{
    		if(arr[i] == '\n')
    		{
    			max = i - 1;
    			max_temp = max;
    			break;
    		}
    	}
    
    	for(i = 0; i < max / 2 + 1; i++)
    	{
    		if(arr[i] != arr[max_temp])
    			return 0;
    		else
    			max_temp--;
    	}
    
    	return 1;
    }
    Hi all, I am trying to input a few sentences and check if each sentence is a palindrome without the spaces, commas and other which means we only take into account of alphabets (a-z or A-Z) e.g.
    A car, a man, a maraca.
    A nut for a jar of tuna.
    Are we not drawn onward to new era?

    It should give output 3 e.g. the number of palindrome sentences entered.
    A enter with nothing for the line will end it.

    However, I am not sure how to filter out the spaces and other non-alphabets as inputs, plus my while statements which are aimed at ending inputs when there is an empty line does not seem to work but I felt that it would work. Anyone can explain to me why it does not work?

  2. #2
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    well from my understanding of your code you want the user to enter something along the lines
    "text\n"
    "\n"
    and when 2 consecutive '\n's are found the loop should end, however, because you increment i after the 2nd '\n' is read you end up with

    [\n] at i-2 and [\n] at i-1 and garbage at i
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  3. #3
    Registered User
    Join Date
    Sep 2010
    Posts
    8
    I want him to enter
    "text\n"
    "text\n"
    "text\n"
    "\n" // type this to exit
    3 // output if all 3 sentences are palindromes

  4. #4
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by hobolux View Post
    I want him to enter
    "text\n"
    "text\n"
    "text\n"
    "\n" // type this to exit
    3 // output if all 3 sentences are palindromes
    I see, the behavior would be the same, with the last two characters, both '\n', being located at i-2 and i-1, you are checking i-1 ('\n') and i (garbage), that is why the loop isn't stopping


    edit: actually, the problem looks worse than that,
    Code:
     do
            {
                scanf("%c", &words[i]);
                
                i++;
            } while(words[i] != '\n');
    always checks garbage (the data is entered in words[0] and you check to see if words[1] contains '\n')
    Last edited by ಠ_ಠ; 10-01-2010 at 01:17 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 04:38 PM
  5. problem printf after inserting single list from file
    By jetfreggel in forum C Programming
    Replies: 3
    Last Post: 04-15-2003, 02:30 PM