First thing:
1. You cant define functinos within functions, its not allowed in C / C++.
2.

int main(void)
{
    char palindrome[80];
    int ispalindrome=1;/*boolean value True*/
    int i=0;
    int j=0;

/* get the user input */
    do{

    printf("Enter a string no more than 80 characters ");
    fgets(palindrome,80,stdin);

// call remove_newline () function here

    j = strlen(palindrome) - 1;
    i = 0;

//////////The below one is a function !!!!///////////////////////
// you need to place it out of main( ) and then call it in main
// with the argument palindrome. Better name the string variable 
// palindrome to "input" or "my_string" to avoid confusion.

  /* Remove the trailing newline*/
     void  remove_newline( char* palindrome ){
       char* p = 0 ;
       if( p = strrchr( palindrome, '\n' ) )
       *p = '\0' ;
     }
/////////////////////////////////////////////////////////////

    int string_length=strlen(palindrome);
     if(palindrome[string_length-1]=='?'||palindrome[string_length-1]=='!'
     ||palindrome[string_length-1]=='.'){
         
  /*If the last character is a ?,!or . continue with normal operation*/
   
    j=string_length-1;
    i = 0;

// this part is unnecessary since fgets() makes sure you dont
// accept more than 80 chars from the user.

    if(j>80){
 /*finds out if characters or more than 80*/
    printf("Error your characters cannot be more than 80");
    }

////////////////////

// create a seperate function named palindrome rather than
// doing all the things in main ( ) itself.
 
    while((i <= j && ispalindrome && isalpha(i) && isalpha(j) )){
        if(tolower (palindrome[i]) != tolower(palindrome[j])) {
      ispalindrome = 0;

        } 
        i++;
         j--;
    }
          if(ispalindrome) {
        printf("%s is a palindrome!\n", palindrome);
        }
        else {
    printf("sorry, %s is not a palindrome\n", palindrome);
  } 
     }
     else
         printf("Your input must be terminated by a punctuation mark");
    }
    }while(j>80);
  
return 0;
}

Also do read the link I had posted previously because it contains each and every step on how to preform the palindrome check.

Make updates and repost.

I saw your check but I like the way I did mine without functions so I'm trying to integrate the last character check here it is without the last character check I saw the method somewhere else

char string[80];
  int ispalindrome = 1; 
  int i,j;

  /* get the user input */
  printf("Enter a string no more than 80 characters ");
  gets(string);
  j = strlen(string) - 1;
  i = 0;

   while((i <= j && ispalindrome && isalpha(i) && isalpha(j) )){
    
         if(tolower (string[i]) != tolower(string[j])) {
      ispalindrome = 0;
    } 
    i++;
    j--;
  } 

 
  if(ispalindrome) {
    printf("%s is a palindrome!\n", string);
  }
  else {
    printf(" %s is not a palindrome\n", string);
  } 
}

Hmm.. it is always a good practice to divide teh work to be done by the program into functions... something you will have to acccept as you move on ahead in your course.. but still if you dont want then...

Anyways, there are a few minor problems with your code.

  1. Dont use gets ( ) . It allows the user to enter as many characters as he wants, thereby writing on the memory which is not of the users ( out of bounds array exception ) as well as leaving the input stream dirty.
    Better use fgets( ) the way i mentioned, which is a more robust fuction.

  2. while((i <= j && ispalindrome && isalpha(i) && isalpha(j) ))
    The problem with this code being that if it encounters a non character alphabet, it exits the loop to declare the result instead of ignoring the character. Pull the validation code i.e. the isalpha( ) part inside the while loop.

  3. You are performign some redundant calculations in this part of the code.

    if(tolower (string[i]) != tolower(string[j]))
    {
    ispalindrome = 0;
    }

    If the condition is not satisfied, there is no way the string is going to be palindrome, so why continue with the remaining calculations. Break teh loop prematurely is this condition is voilated.

Make the changes and repost.

I am getting comflicting information right now but i think i'm going to leave out the termination I just don't think it is necessary if it is phrases like
Ah! satan sees natasha will be invalid and that just isn't sensible that can't be what he means I think the method I used satisfies my assumptions except for the comma not being used as a terminating mark. Do you see what I mean

I am getting comflicting information right now

...which is ?

but i think i'm going to leave out the termination I just don't think it is necessary if it is phrases like Ah! satan sees natasha will be invalid

Dont decide things your way, the assignment states that "all " palindromes should be verified.

and that just isn't sensible that can't be what he means I think

No he means exactly that thing. You are assuming wrong things.

Do you see what I mean

I see that you are trying to give up on this .

Post your problems.

:rolleyes: ok its amazingly scary how u saw through my guise...Its just getting too complicated, i haven't even attempted the histogram thingy yet and a friend of mine told me I was thinking too hard so I tried to cop out. But I'll try...

while((i <= j && ispalindrome && isalpha(i) && isalpha(j) ))

how do I extract the isalpha() do I use another loop directly after

while((i <= j && ispalindrome ))
 if((isalpha(i) && isalpha(j))

and where to i make sure the two letters being compared are not case sensitive if not there?

:rolleyes: ok its amazingly scary how u saw through my guise...Its just getting too complicated,

Knock knock... I am the moderator... remember ...:D

while((i <= j && ispalindrome ))
{
     while ( ! isalpha( my_string[i] ) ) // you had forgotten to write the string name
     {
          ++ i ;  // move ahead
     }

     while( ! isalpha( my_string[j] ) ) 
     {
           --j ;
     }
     
     // perform check here and exit if not same
}

Incorporate this somethign like this in your code and repost.

PS: Its almost 3.30 AM here so now I would be signing off. would solve your queries tomo. Bye.

Ok, can I ask where I would include a provision for the comma?

/* function definition*/
	 void  remove_newline( char* input ){
      char* p = 0 ;
	   if( p = strrchr( input, '\n' ) )
       *p = '\0' ;


int main(void)
{
	char my_string[80];
	int ispalindrome=1;/*boolean value True*/
	int i=0;
	int j=0;
	void  remove_newline( char* input); 


/* get the user input */
	do{

	printf("Enter a string no more than 80 characters ");
	fgets(palindrome,80,stdin);
	j = strlen(palindrome) - 1;
	i = 0;

	/*remove trailing line*/
  void  remove_newline( char* my_string)

	 int string_length=strlen(my_string);
	 if(my_string[string_length-1]=='?'||my_string[string_length-1]=='!'
	 ||my_string[string_length-1]=='.'){
		 
  /*If the last character is a ?,!or . continue with normal operation*/
   
	j=string_length-1;
	i = 0;

	if(j>80){
 /*finds out if characters are more than 80*/
	printf("Error your characters cannot be more than 80");
	}

	while((i <= j && ispalindrome )){
     while ( ! isalpha( my_string[i] ) ) 
     {
          ++ i ;  
     }

     while( ! isalpha( my_string[j] ) ) 
     {
           --j ;
     }

	if(tolower (my_string[i]) != tolower(my_string[j])) {
      ispalindrome = 0;

		} 
		i++;
		 j--;
	}
  		if(ispalindrome) {
		printf("%s is a palindrome!\n", palindrome);
		}
		else {
    printf("sorry, %s is not a palindrome\n", palindrome);
  } 
	 }
	 else
		 printf("Your input must be terminated by a punctuation mark");
	
	}while(j>80);
  
return 0;
}

OK, it's time to back up and (sort of) start over. I haven't gone thru all your code, but you seem to be getting lost trying to solve 5 things at once. You never get anywhere that way.

Keep what you have because you can probably use most of it. Let's just rearrange stuff and do one step at a time.

#1:
Start with a new project.
Write the input portion of the code.
Input a string.
Output the string
When the string input and output are correct, move on, but not before!

#2:
Add the test whether the last character of the string is correct.
If not, do what you think you need to do.
Look at what you already have and use what you can.
When you can test the sentence correctly, move on, but not until!

#3:
Copy the input into another string character by character.
Copy only the characters you want to test as a palindrome.
Do not copy characters that aren't palindromish (space, comma, etc)
If you use string you should be OK. If you used char be sure to add that ending '\0'
Print out what you copied. When this looks correct, move on, but not before!

#4:
Add the ability during the copy to convert all uppercase characters to lower case.
Print out what you copied. When this looks correct, move on, but not until!

#5:
Now test for the palindrome.

Umm ...whats wrong with what I posted before?I know I'm being lazy but I don't really wanna go through the motions right now this is due tomorrow . Could you just point out the major flaws?

Umm ...whats wrong with what I posted before?

Well, maybe:

* how do I extract the isalpha() do I use another loop ...
* where to i make sure the two letters being compared are not case sensitive...
* where I would include a provision for the comma?
* i'm going to leave out the termination I just don't think it is necessary....
etc

and your code still doesn't work

I know I'm being lazy but I don't really wanna go through the motions right now .

And how is this incentive for us to help you further? We aren't being lazy... :rolleyes:
Sheesh!

Ok you're right I'll show some enthusiam, I tried to do everything you said (from what I could undertand) but it is not searching for the last character because it reports every string as not having punctuation.

int main(void)
{
	char my_string[80];
	int ispalindrome=1;/*boolean value True*/
	int i=0;
	int j=0;
	void remove_newline (char *); 


/* get the user input */
	

	printf("Enter a string no more than 80 characters ");
	fgets(my_string,80,stdin);

	void remove_newline (char *my_string);

	
	int string_length=strlen(my_string);

	 if(my_string[string_length-1]=='?'||my_string[string_length-1]=='!'
	 ||my_string[string_length-1]=='.'){
		 
  /*If the last character is a ?,! or . continue with normal operation*/
   
	j=string_length-1;
	i = 0;

	
	while((i <= j && ispalindrome )){
     while ( ! isalpha( my_string[i] ) ) 
     {
          ++ i ;  
     }

     while( ! isalpha( my_string[j] ) ) 
     {
           --j ;
     }

		if(tolower (my_string[i]) != tolower(my_string[j])) {
      ispalindrome = 0;

		} 
		i++;
		 j--;
	}
  		if(ispalindrome) {
		printf("%s is a palindrome!\n", my_string);
		}
		else {
    printf("sorry, %s is not a palindrome\n", my_string);
  } 
	 }
	 else{
		 printf("Your input must be terminated by a punctuation mark\n");
	 }
	
  
return 0;
}
void remove_newline (char *ch)
 
{
	char *s;
	s = (char *) strchr(ch,'\n');
	if (s)
    *s='\0';
}
int main(void)
{
    char my_string[80];
    int ispalindrome=1;/*boolean value True*/
    int i=0;
    int j=0;
    void remove_newline (char *); 


/* get the user input */
    

    printf("Enter a string no more than 80 characters ");
    fgets(my_string,80,stdin);

     void remove_newline (char *my_string);
 
    
    int string_length=strlen(my_string);

     if(my_string[string_length-1]=='?'||my_string[string_length-1]=='!'
     ||my_string[string_length-1]=='.'){
         
  /*If the last character is a ?,! or . continue with normal operation*/
   
    j=string_length-1;
    i = 0;

    
    while((i <= j && ispalindrome )){
     while ( ! isalpha( my_string[i] ) ) 
     {
          ++ i ;  
     }

     while( ! isalpha( my_string[j] ) ) 
     {
           --j ;
     }

        if(tolower (my_string[i]) != tolower(my_string[j])) {
      ispalindrome = 0;

        } 
        i++;
         j--;
    }
          if(ispalindrome) {
        printf("%s is a palindrome!\n", my_string);
        }
        else {
    printf("sorry, %s is not a palindrome\n", my_string);
  } 
     }
     else{
         printf("Your input must be terminated by a punctuation mark\n");
     }
    
  
return 0;
}
void remove_newline (char *ch)
 
{
    char *s;
    s = (char *) strchr(ch,'\n');
    if (s)
    *s='\0';
}

Does your code even compile ? which compiler are you using ? Wait let me guess.. is it Turbo C ?

its compiling 0 error 0 warning... i'm using visual studio why is this highlighted?

void remove_newline (char *my_string);

Ah my bad.. maybe I am just going senile. Not used to writing function prototypes in main( ) and would advise you the same.

"Keep the function prototypes outside main ( ) preferably after the includes and before main ( )"

And now to the main part, what kind of errors or bugs where you gettting ?

1)It isn't reading the punctuation mark
deed!
deed
yields the same error message

2)I'm triyng to incorprate a histogram(like the one below) if it is a palindrome but I think using fgets() makes this task harder since I need to check each character and count the freqency
1 2 3 4 5
d|**
e|**

I've been going over this all night and time is slipping away...:sad:

Okay you are not callling the remove_newline( ) function. And you havent moved the function prototype outside main( ). If you dont listen closely to what I say, it would really discourage me from helping you out, coz it makes me think my efforts on you are going to waste.

Make changes and then try to test the code. I think the newline is causing problems, call remove newline and it should start working.

I thought I called the function here:

void remove_newline (char *my_string);

(forgive me I'm rusty on functions)
I moved the prototype and the definition outside of main but nothing...

I thought I called the function here:

void remove_newline (char *my_string);

NO this is a function prototype, which I asked you to move outside main(). And you call a function with something like:

(return variable = )function( parameters ) ;

Call the function after accepting input from the user.

okay using this

string=remove_newline(char*my_string);

getting a whole bunch of syntax errors so I'm obviously not calling it right

Hmm dont you have a reference book which you learn from. Please read the things required for your assignment before you ask for help.. that way it makes our job easy .

BTW I will not give you direct answer, just a hint,

A function consists of
1. Function name
2. Return type
3. Function parameters

To call a function, its call should go along with the defination or prototype of the function.

Your function prototype is : void remove_newline( char* input ) ; while your function call is: string = remove_newline( char* my_string ) ; Dont you think something is wrong here. Also once you specify the datatype of the argument which your function accpets in the prototype, you need not specify it again in the functino call.

Think a bit, refer notes and try to come up with the best possible answer.

Forget the remove_newline() function. Make it simple:

fgets(my_string,80,stdin);

    int string_length=strlen(my_string);

    if (my_string[string_length-1]=='\n')
    {
        string_length--;
        my_string[string_length] = 0;
    }

    if (my_string[string_length-1]=='?' || 
        my_string[string_length-1]=='!' ||
        my_string[string_length-1]=='.')
    {

Also, you can use cout to see what's happening. Display the character you are testing to see if you have the correct one. If it's not, check your code to see why not. cout is a good debugging tool.

Works! I just needed to do a little reading I made something so simple pretty painful didn't I?

Now If only I good get help with printing my histogram.....;)

Post your updated code and tell us what you have attempted till now for histogram.

sorry

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

void remove_newline (char* input);/*Prototype*/


int main(void)
{
    char my_string[80];
    int ispalindrome=1;/*boolean value True*/
    int i=0;
    int j=0;
	char string[80];
     


/* get the user input */
    

    printf("Enter a string no more than 80 characters \n");

    fgets(my_string,80,stdin);

	remove_newline( my_string );/*Function call*/

     int string_length=strlen(my_string);

     /*If the last character is a ?,! or . continue with normal operation*/

     if(my_string[string_length-1]=='?'||my_string[string_length-1]=='!'
     ||my_string[string_length-1]=='.'){
         
  
   
    j=string_length-1;
    i = 0;

    
    while((i <= j && ispalindrome )){
     while ( ! isalpha( my_string[i] ) ) 
     {
          ++ i ;  
     }

     while( ! isalpha( my_string[j] ) ) 
     {
           --j ;
     }

        if(tolower (my_string[i]) != tolower(my_string[j])) {
      ispalindrome = 0;

        } 
        i++;
         j--;
    }
          if(ispalindrome) {
        printf("%s is a palindrome!\n", my_string);
       /*Insert histogram here*/

        }
        else {
    printf("sorry, %s is not a palindrome\n", my_string);
  } 
     }
     else{
         printf("Your input must be terminated by '!','?','.' \n");
     }
    
  
return 0;
}
void remove_newline (char *input) /*definition
 
{
    char *s;
    s = (char *) strchr(input,'\n');
    if (s)
    *s='\0';
}

Tsk tsk... are you not forgetting something ?

Hint: Code tags, your histogram attempt, your current problems

for my histogram i inserted this code below the if with the histogram

printf("%13s%13s%13s%13s%13s\n","10","20","30","40","50");
     for(int k=0;k<80;k++){
          printf("%7c%13c%      ",k,string[k]);

          for (int l=1; l<=string[i];l++){
               printf("%c",'*');
          }
          printf("\n");
     }

I got this from a book but i think it only applies to numbers

No, it more or less applies to both numbers as well as characters since in the end what you are doing is displaying the contents of an array which holds the frequency of occurance of something.

Your main concern here is to count the alphabets. It should be very simple:

1. Create an integer array with size 128 ( since we need only alphabets, we could have made it smaller, but no need to save a few bits ) and set it all to 0.
2. In the part where you check whether the character under consideration is a alphabet or not, update the value of the array to reflect that the alphabet has occured once. For eg. if the condition in your palindrome loop says that the current character is an alphabet, increase its frequency in the frequency table( use the relation between characters and integers for this thing)
3. At the end of the palindrome loop if the given string is a palidrome, display the frequency of each character using the code which you pasted above (of courese with some modifications)

Attemp atleast this and repost.

No, it more or less applies to both numbers as well as characters since in the end what you are doing is displaying the contents of an array which holds the frequency of occurance of something.

Your main concern here is to count the alphabets. It should be very simple:

1. Create an integer array with size 128 ( since we need only alphabets, we could have made it smaller, but no need to save a few bits ) and set it all to 0.

Done

2. In the part where you check whether the character under consideration is a alphabet or not, update the value of the array to reflect that the alphabet has occured once. For eg. if the condition in your palindrome loop says that the current character is an alphabet, increase its frequency in the frequency table( use the relation between characters and integers for this thing)

What does this look like? How does it know which leeter has been updated?

while((i <= j && ispalindrome )){
     while ( ! isalpha( my_string[i] ) ) 
     {
          ++ i ;
		  alpha[128]+=1
     }

     while( ! isalpha( my_string[j] ) ) 
     {
           --j ;	
		alpha[128]+=1

3. At the end of the palindrome loop if the given string is a palidrome, display the frequency of each character using the code which you pasted above (of courese with some modifications)

I can only guess at the modifications except for maybe that k has to represent only one of the letters in the string

Attemp atleast this and repost.

I think i'll stop here I've got 30 minutes left but thanks for everthing

alpha[128] += 1 ; I dont know what you are trying to do here, but if the array if of 128 elements you can only do till my_array[127] since the arrays are zero indexed i.e. the indexing of arrays starts at 0 ( due to internal represenetation being of pointer type).

Also as you already must be knowing you can use a variable to index into the array like: my_array[i] = some_value ; Use the same trick , but in your case instead of 'i' you use the integer value of the character encountered.

For eg. my_array['a'] += 1 ; will increment the value of my_array[97] since 'a' is equivalent to 97 (based on the ascii table)

Use this trick to increase the counter as soon as you encounter an alphabet.

PS: I am attaching the ASCII table for your referncce.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.