Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,491 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,290 people online right now. Registration is fast and FREE... Join Now!




Using ctype.h and ispunct()

 
Reply to this topicStart new topic

Using ctype.h and ispunct(), I can't get the punctuation count to increment

dr_worm
28 Feb, 2007 - 11:53 AM
Post #1

D.I.C Head
**

Joined: 11 Sep, 2006
Posts: 131


My Contributions
This is basically a ROT2 encryption program that actually only deals with letters and interchanging certain punctuation characters. The problem I have is that I get ridiculous number for the number of punctuation chars I'm supposed to count up. Maybe I'm implementing the ispunct() function wrong, or I'm not looking at the right variable. Who knows?

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

char convert( char );

int main () {

  char line[96], *line_ptr, output[96];
  int index, lines_processed, punct_chars;
  lines_processed = 0;
  punct_chars = 0;

  for( lines_processed = 0; 1; ++lines_processed ) {

    printf( "Input a line> " );
    line_ptr = fgets( line, 96, stdin );
    if( line_ptr != line || line_ptr[0] == EOF ) break;
    for( index = 0; index < 96; ++index ) {
      if( ispunct( (int)line_ptr[index] ) ) ++punct_chars;
      output[index] = convert( line_ptr[index] );}
    printf( "%s%s", line_ptr, output );
  }

  printf( "\n%d lines processed, %d punctuation characters.\n", lines_processed, punct_chars );

  return 0;
}

char convert( char input ) {

  if( ((int)input >= 'A' && (int)input < 'Y') || ((int)input >= 'a' && (int)input < 'y') ) { input+= 2;}
  else if( (int)input == 'Y' || (int)input == 'Z' || (int)input == 'y' || (int)input == 'z' ) { input-= 24;}
  
  if( (int)input == '(' ) { input+= 1;}
  else if( (int)input == ')' ) { input-= 1;}
  
  if( (int)input == '[' || (int)input == '{' || (int)input == '<' ) { input+= 2;}
  else if( (int)input == ']' || (int)input == '}' || (int)input == '>' ) { input-= 2;}

  return input;
}

I haven't commented the code yet, but it should be a pretty straightforward design. If need be I can repost once I comment it.
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Using Ctype.h And Ispunct()
28 Feb, 2007 - 01:03 PM
Post #2

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,349



Thanked: 51 times
Dream Kudos: 25
My Contributions
The ispunct() function returns non-zero if its argument is a printing character but neither alphanumeric nor a space. Otherwise, zero is returned.

Have you checked the return value? Perhaps it's not being interpreted correctly.

Have you tried NOT casting the argument to the function as an int?
User is online!Profile CardPM
+Quote Post

dr_worm
RE: Using Ctype.h And Ispunct()
28 Feb, 2007 - 01:20 PM
Post #3

D.I.C Head
**

Joined: 11 Sep, 2006
Posts: 131


My Contributions
I thought that this would evaluate to true (non-zero):
CODE
ispunct((int)line_ptr[index]);

And increment punct_chars. I get the same result with:
CODE
if( ispunct((int)line_ptr[index]) != 0) ++punct_chars;

And if I don't cast int, I get a warning for subscript has type char.

This post has been edited by dr_worm: 28 Feb, 2007 - 01:22 PM
User is offlineProfile CardPM
+Quote Post

dr_worm
RE: Using Ctype.h And Ispunct()
28 Feb, 2007 - 04:05 PM
Post #4

D.I.C Head
**

Joined: 11 Sep, 2006
Posts: 131


My Contributions
Quick update:

So I have this:
CODE

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

int main() {
  int i;
  char c[6]= "Not?!";
  for( i=0; i<5; ++i ) {
    if( ispunct((int)c[i])) printf("True! "); else printf("False! ");
  }
  return 0;
}


For which the return is "False! False! False! True! True!", as it should be.

This still boggles me:
CODE

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

int main () {
  char line[96], *line_ptr;
  int index, lines_processed, punct_chars;
  punct_chars = NULL;

  for( lines_processed = 0; 1; ++lines_processed ) {

    line_ptr = fgets( line, 96, stdin );
    if( line_ptr != line ) break;
    for( index = 0; index < 96; ++index ) {
      if( ispunct((int)line_ptr[index])) ++punct_chars;
  }

  printf( "\n%d lines processed, %d punctuation characters.\n", lines_processed, punct_chars );
  return 0;
}

If I feed it "Not?!", I get 1 line processed, 6 punctuation characters. What am I missing?
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 05:31PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month