User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 403,490 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,272 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 5224 | Replies: 63
Reply
Join Date: May 2006
Posts: 2,700
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 14
Solved Threads: 220
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Terminating a String

  #41  
Nov 6th, 2006
Originally Posted by boujibabe View Post
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


Originally Posted by boujibabe View Post
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!
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Terminating a String

  #42  
Nov 6th, 2006
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';
}
Last edited by boujibabe : Nov 6th, 2006 at 6:05 am.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,816
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Terminating a String

  #43  
Nov 6th, 2006
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 ?
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Terminating a String

  #44  
Nov 6th, 2006
its compiling 0 error 0 warning... i'm using visual studio why is this highlighted?

void remove_newline (char *my_string);
Last edited by boujibabe : Nov 6th, 2006 at 11:02 am.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,816
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Terminating a String

  #45  
Nov 6th, 2006
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 ?
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Terminating a String

  #46  
Nov 6th, 2006
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...
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,816
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Terminating a String

  #47  
Nov 6th, 2006
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.
Last edited by ~s.o.s~ : Nov 6th, 2006 at 11:59 am.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Terminating a String

  #48  
Nov 6th, 2006
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...
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,816
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 339
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: Terminating a String

  #49  
Nov 6th, 2006
Originally Posted by boujibabe View Post
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.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Nov 2004
Posts: 123
Reputation: boujibabe is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
boujibabe boujibabe is offline Offline
Junior Poster

Re: Terminating a String

  #50  
Nov 6th, 2006
okay using this

string=remove_newline(char*my_string);

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

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the C Forum

All times are GMT -4. The time now is 12:13 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC