Chapter 11. CHARACTER STRINGS AND STRING FUNCTIONS

I l @ ve RuBoard

Chapter 11. CHARACTER STRINGS AND STRING FUNCTIONS

You will learn about the following in this chapter:

  • Functions

     gets(), puts(), strcat(), strncat(), strcmp(), strncmp() strcpy(), strncpy(), sprint(), strchr(), isalnum() isalpha(), iscntrl(), isdigit(), isgraph() islower(), isprint(), ispunct(), isspace() isupper(), isxdigit() 

In this chapter, you learn more about creating and using strings. You use several string and character functions from the C library and practice creating your own string functions. Also, you learn how to use command-line arguments.

Character strings are one of the most useful and important data types in C. You have been using character strings all along, but there still is much to learn about them. Of course, you already know the most basic fact: A character strings is a char array terminated with a null character ( \0 ). This chapter discusses the nature of strings, how to declare and initialize strings, how to get them into and out of programs, and how to manipulate strings.

Listing 11.1 presents a busy program that illustrates several ways to set up strings, read them, and print them. It uses two new functions: gets() , which reads a string, and puts() , which prints a string. (You probably notice a family resemblance to getchar () and putchar () .) The rest of the program should look fairly familiar.

Listing 11.1 The strings.c program.
 /* strings.c -- stringing the user along */ #include <stdio.h> #define MSG "You must have many talents. Tell me some."                           /* a symbolic string constant */ #define LIM  5 #define LINELEN 81        /* maximum string length + 1  */ char m1[] = "Just limit yourself to one line's worth.";                           /* initializing an external   */                           /* character array            */ char * m2 = "If you can't think of anything, fake it.";                           /* initializing an external   */                           /* character pointer          */ int main(void) {   char name[LINELEN];   char talents[LINELEN];   int i;   char *m3 = "\nEnough about me -- what's your name?";                           /* initializing a pointer       */   char *mytal[LIM] = {  "Adding numbers swiftly",           "Multiplying accurately", "Stashing data",           "Following instructions to the letter",           "Understanding the C language"};                           /* initializing an array of     */                           /* string pointers              */   printf("Hi! I'm Clyde the Computer. I have many talents.\n");   printf("Let me tell you some of them.\n");   puts("What were they? Ah, yes, here's a partial list.");   for (i = 0; i < LIM; i++)        puts(mytal[i]);  /* print list of computer talents */   puts(m3);   gets(name);   printf("Well, %s, %s\n", name, MSG);   printf("%s\n%s\n", m1, m2);   gets(talents);   puts("Let's see if I've got that list:");   puts(talents);   printf("Thanks for the information, %s.\n", name);   return 0; } 

To show you what this program does, here is a sample run:

 Hi! I'm Clyde the Computer. I have many talents. Let me tell you some of them. What were they? Ah, yes, here's a partial list. Adding numbers swiftly Multiplying accurately Stashing data Following instructions to the letter Understanding the C language Enough about me -- what's your name?  Nigel Barntwit  Well, Nigel Barntwit, You must have many talents. Tell me some. Just limit yourself to one line's worth. If you can't think of anything, fake it.  Fencing, yodeling, malingering, cheese tasting, and sighing.  Let's see if I've got that list: Fencing, yodeling, malingering, cheese tasting, and sighing. Thanks for the information, Nigel Barntwit. 

Let's see how Listing 11.1 works. However, rather than go through it line by line, let's take a more encompassing approach. First, you will look at ways of defining a string within a program. Then you will see what is involved in reading a string into a program. Finally, you will study ways to output a string.

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