The ctype.h Character Functions and Strings

I l @ ve RuBoard

The ctype .h Character Functions and Strings

Chapter 7, "C Control Statements: Branching and Jumps," introduced the ctype.h family of character- related functions. These functions can't be applied to a string as a whole, but they can be applied to the individual characters in a string. Listing 11.26, for example, defines a function that applies the toupper() function to each character in a string, thus converting the whole string to uppercase. It also defines a function that uses ispunct () to count the number of punctuation characters in a string.

Listing 11.26 The convert.c program.
 /* convert.c -- modifies a string */ #include <stdio.h> #include <string.h> #include <ctype.h> #define LIMIT 80 void ToUpper(char *); int PunctCt(const char *); int main(void) {   char line[LIMIT];   puts("Please enter a line:");   gets(line);   ToUpper(line);   puts(line);   printf("That line has %d punctuation characters.\n",          PunctCt(line));   return 0; } void ToUpper(char * str) {   while (*str != ` 
  /* convert.c -- modifies a string */ #include <stdio.h> #include <string.h> #include <ctype.h> #define LIMIT 80 void ToUpper(char *); int PunctCt(const char *); int main(void) { char line[LIMIT]; puts("Please enter a line:"); gets(line); ToUpper(line); puts(line); printf("That line has %d punctuation characters.\n", PunctCt(line)); return 0; } void ToUpper(char * str) { while (*str != `\0') { *str = toupper(*str); str++; } } int PunctCt(const char * str) { int ct = 0; while (*str != `\0') { if (ispunct(*str)) ct++; str++; } return ct; }  
') { *str = toupper(*str); str++; } } int PunctCt(const char * str) { int ct = 0; while (*str != `
  /* convert.c -- modifies a string */ #include <stdio.h> #include <string.h> #include <ctype.h> #define LIMIT 80 void ToUpper(char *); int PunctCt(const char *); int main(void) { char line[LIMIT]; puts("Please enter a line:"); gets(line); ToUpper(line); puts(line); printf("That line has %d punctuation characters.\n", PunctCt(line)); return 0; } void ToUpper(char * str) { while (*str != `\0') { *str = toupper(*str); str++; } } int PunctCt(const char * str) { int ct = 0; while (*str != `\0') { if (ispunct(*str)) ct++; str++; } return ct; }  
') { if (ispunct(*str)) ct++; str++; } return ct; }

The while (*str != `\0') loop processes each character in the string pointed to by str until the null character is reached. Here is a sample run:

 Please enter a line:  Me? You talkin' to me? Get outta here!  ME? YOU TALKIN' TO ME? GET OUTTA HERE! That line has 4 punctuation characters. 

The ToUpper() function applies toupper() to each character in a string. (The fact that C distinguishes between uppercase and lowercase makes these two function names different from one another.) As defined by ANSI C, the toupper() function alters only characters that are lowercase. However, very old implementations of C don't do that check automatically, so old code normally does something like this:

 if (islower(*str))     /* pre-ANSI C -- check before converting */     *str = toupper(*str); 

Incidentally, the ctype.h functions are usually implemented as macros . These are C preprocessor constructions that act much like functions, but have some important differences. We'll cover macros in Chapter 16.

Next, let's try to fill an old emptiness in our lives, namely, the void between the parentheses in main() .

I l @ ve RuBoard


C++ Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 314
Authors: Stephen Prata

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net