13 - Lecture 13| Strings and Pointers The End-Of-String...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture 13| Strings and Pointers The End-Of-String Sentinel 'n0' Initialization of Strings An Example String Handling Functions in the Standard Library Checking String Length | strlen() Displaying Strings | puts() Getting Strings | scanf() Getting Strings | gets() Copying Strings | strcpy() Copying Strings | strncpy() Concatenating Strings | strcat() Comparing Strings | strcmp() String Conversion Functions Remark Strings and Pointers Readings Exercises CSC 1500 { Lecture 13 1 Overview In C, a string is a one-dimensional array of type char. The character value 'n0' (ASCII value zero) is used to terminate a string (indicating the end of a string). The standard library provides many useful string handling functions. CSC 1500 { Lecture 13 2 The End-Of-String Sentinel 'n0' #include <stdio.h> int main(void) { char name 15]="C Program" printf("Hello, %s\n", name) return 0 } Hello, C Program Address 400 401 402 403 404 405 406 407 name i] 01234567 Character C Progra ASCII value 67 32 80 114 111 103 114 97 Note: characters in the string name name 408 409 410 411 412 413 414 8 9 10 11 12 13 14 m n0 ? ? ? ? ? 109 0 ? ? ? ? ? NULL Unused is a character array. can also be viewed as a string since it is terminated by the NULL character. NULL is called a sentinel which signi es the end of the meaningful part of the array comprising the string. NULL is prede ned as 'n0'. CSC 1500 { Lecture 13 3 Initialization of Strings As a literal string with the size speci ed explicitly: char name 15]="C Program" 9] The size of the char array is 15 with name initialized to 'n0'. char name ]="C Program" automatically As a literal string without specifying the array size explicitly: The size of the char array is the length of the string plus one, that is, 10. name 9] is automatically initialized to 'n0'. In fact, it is equivalent to: char name ] = {67, 32, 80, 114, 111, 103, 114, 97, 109, 0} OR: char name ] = {'C',' ','P','r','o','g','r','a','m','\0'} CSC 1500 { Lecture 13 4 An Example #include <stdio.h> #define MAXLEN 20 int main(void) { char name MAXLEN+1] int i, len /* maximum size +1 cater for the '\0' */ printf ("Hi! What's your name (at most 20 characters, without space)? ") scanf("%s", name) /* note that no '&' is placed before name */ for (i=0, len=0 /* check the length */ /* an empty-body for-loop */ printf("%-25s%2d\n", "Length of your name is:", len) printf("%-25s", "Your name reversed is:") for (i=len-1 i>=0 i--) printf("%c", name i]) printf ("\n") return 0 } /* print the reversed name */ name i]!='\0' i++, len++) Hi! What's your name (at most 20 characters, without space)? C-Program Length of your name is: 9 Your name reversed is: margorP-C is declared as having a size of MAXLEN+1, that is, 21. It can accommodate a string of at most 20 characters. One extra space is reserved for the 'n0'. scanf() copies the characters you typed into the addresses starting at where you provide, i.e. name. scanf() automatically places a 'n0' into the address immediately after where the last character is stored. name Is there any convenient string handling method? CSC 1500 { Lecture 13 5 String Handling Functions in the Standard Library The standard library contains many useful string handling functions. They all require that strings passed as arguments be nullterminated. Their function prototypes are given in the header le <string.h>. <string.h> <string.h> <string.h> <string.h> <string.h> <string.h> <string.h> <string.h> <string.h> <string.h> <string.h> <string.h> <string.h> <string.h> <string.h> <string.h> CSC 1500 { Lecture 13 6 Checking String Length | strlen() Function Prototype: #include <string.h> typedef unsigned int size_t /* predefined */ size_t strlen(const char *s) Function Description: Determines the length of string s. The number of characters preceding the termination NULL character is returned. Example: #include <stdio.h> #include <string.h> int main(void) { char s ]="This is a string!" printf("%d\n", strlen(s)) return 0 } 17 CSC 1500 { Lecture 13 7 Displaying Strings | puts() Function Prototype: #include <stdio.h> int puts(const char *s) Function Description: Print the string s followed by a newline character. It returns the number of characters written otherwise it returns EOF on failed operation. Example: #include <stdio.h> #include <string.h> int main(void) { char puts(s) return 0 } s ]="This is a string!" This is a string!<newline> You should know how to use printf() for displaying strings. CSC 1500 { Lecture 13 8 Getting Strings | scanf() Example: #include <stdio.h> int main(void) { char s1 30], s2 10] /* the arrays should have enough space */ scanf("%s", s1) printf("s1=%s\n", s1) printf("\n") scanf("%s", s2) printf("s2=%s\n", s2) return 0 } this-is-first-string. s1=this-is-first-string. this is second string. s2=this When inputing a string using scanf(), the argument does not need to be preceded by '&'. White spaces are regarded as delimiters by scanf(). Thus, scanf() cannot be used to input a string containing space characters. CSC 1500 { Lecture 13 9 Getting Strings | gets() Function Prototype: #include <stdio.h> char *gets(char *s) Function Description: Input characters from the standard input into the array s until a newline or end-of- le character is encountered. A terminating NULL character is appended to the array. The array s is returned. Example: #include <stdio.h> #include <string.h> int main(void) { char name 20] /* assume name = effffc04 */ printf("Your name? ") printf("Hello %s!\n", gets(name)) /* gets(name) returns value of name, i.e. effffc04 */ /* therefore the above statement is equivalent to: gets(name) printf("Hello %s!\n", name) return 0 } */ Your name? C Program Hello C Program! CSC 1500 { Lecture 13 10 Copying Strings | strcpy() Function Prototype: #include <string.h> char *strcpy(char *s1, const char *s2) Function Description: Copies the string s2 into the array s1. afterwards. The array s1 is returned. s1 is null-terminated Example: #include <stdio.h> #include <string.h> int main(void) { char char s1 20] s2 20]="Hello world!" /* uninitialized */ strcpy (s1, s2) printf("%s\n", s1) return 0 } Hello world! CSC 1500 { Lecture 13 11 Copying Strings | strncpy() Function Prototype: #include <string.h> char *strncpy(char *s1, const char *s2, size_t n) Function Description: Copies at most n characters of the string s2 into the array s1. Rest of s2 is ignored if necessary. s1 will not be null-terminated if the length of s2 equals n or more. The array s1 is returned. Example: #include <stdio.h> #include <string.h> int main(void) { char char char s_short 6] s2 20]="Hello world!" s_long 30] strncpy(s_short, s2, 5) s_short 5]='\0' /* copies s2 0]...s2 4] to s_short */ /* s2 is longer than 5 character, s1 is not null-terminated */ printf("%s\n\n", s_short) strncpy(s_long, s2, 29) /* s_long is null-terminated in this case */ printf("%s\n\n", s_long) return 0 } Hello Hello world! CSC 1500 { Lecture 13 12 Concatenating Strings | strcat() Function Prototype: #include <string.h> char *strcat(char *s1, const char *s2) Function Description: Appends the string s2 to the array s1. The rst character of s2 overwrites the terminating NULL character of s1. The value of s1 is returned. Example: #include <stdio.h> #include <string.h> int main(void) { char char s1 20]="Happy " s2 ]="New Year" printf("s1=%s, s2=%s\n", s1, s2) printf("s1+s2=%s\n", strcat(s1, s2)) return 0 } s1=Happy, s2=New Year s1+s2=Happy New Year CSC 1500 { Lecture 13 13 Comparing Strings | strcmp() Function Prototype: #include <string.h> int strcmp(const char *s1, const char *s2) Compares the string s1 to the string s2. The function returns 0, less than 0, or greater than 0 if s1 is equal to, less than, or greater than s2, respectively. Function Description: Example: #include <stdio.h> #include <string.h> int main(void) { char myname ]="C Program" char yourname 20] int code printf("Your name? ") gets(yourname) code = strcmp(yourname, myname) if (code > 0) printf("You are bigger!\n") else if (code < 0) printf("You are smaller!\n") else printf("Same!\n") return 0 } Your name? C Program Same! Your name? Au Yeung Siu Ming You are smaller! Your name? Wong Tai Man You are bigger! Your name? 123 You are smaller! CSC 1500 { Lecture 13 14 String Conversion Functions | atoi(), atof(), atol() Function Prototype: #include <stdlib.h> int long atoi(const char *s) atol(const char *s) double atof(const char *s) The functions examines the string s to produce a numerical value of type int, double, and long, respectively. Function Description: Example: #include <stdio.h> #include <stdlib.h> int main(void) { char s 20] printf("Input a number: ") scanf("%s", s) printf("String: %s\n", s) printf("Integer: %d\n", atoi(s)) printf("Double: %.2f\n", atof(s)) printf("Long: %ld\n", atol(s)) return 0 } Input a number: 1385.97 String: 1385.97 Integer: 1385 Double: 1385.97 Long: 1385 CSC 1500 { Lecture 13 15 Remark There are many other string handling functions in the string library and standard library. Check the documentation to see if an existing function suits your need before attempting to write your own string handling functions. Remember, browsing through reference manuals is the essential technique that every C programmer should equip with. Beware! Please make sure your char arrays have enough storage space for handling the strings, don't forget to reserve a space to the string-terminator, the NULL char. Most of the string input/output/handling functions (e.g. scanf()/printf()/strcpy()/...) expects argument strings with terminator. CSC 1500 { Lecture 13 16 Strings and Pointers #include <stdio.h> #include <string.h> int main(void) { char char char name ]="C Program" message 256]="Hello, " *cptr printf("%s\n", strcat(message, name)) cptr = message + 7 printf("%s\n", cptr) return 0 } Hello, C Program C Program message cptr H e l l o , C P r o g r a m \0 ...... You can exibly use char pointers to manipulate strings. Handling sub-strings using pointers is particularly easy. CSC 1500 { Lecture 13 17 Readings Chapter 10, Sections 10.1 { 10.5 Chapter 10, Sections 10.7 { 10.9 Appendix, A.14 Exercises Chapter 10, Exercises 1, 3, 10 2 End of Lecture 13 CSC 1500 { Lecture 13 18 ...
View Full Document

This note was uploaded on 05/23/2010 for the course COMPUTER S CSC1500 taught by Professor Fung during the Spring '10 term at CUHK.

Ask a homework question - tutors are online