STRINGS 305171 Computer Programming Rattapoom Waranusast Department of

  • Slides: 32
Download presentation
STRINGS 305171 Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of

STRINGS 305171 Computer Programming Rattapoom Waranusast Department of Electrical and Computer Engineering Faculty of Engineering, Naresuan University

Strings are groups of letters, numbers, and many other characters. Ø We can create

Strings are groups of letters, numbers, and many other characters. Ø We can create and initialize a string using a character array and a terminating NULL character, as shown next. Ø char my. Str[5] = {‘T’, ‘e’, ‘s’, ‘t’, ‘’}; 2

When creating character arrays, it is important to allocate enough room for the NULL

When creating character arrays, it is important to allocate enough room for the NULL character (‘’) because many C library functions look for the NULL character when processing character arrays. Ø If the NULL character is not found, some C library functions may not produce the desired result. Ø 3

A string variable can also be created and initialized with a string literal (constant).

A string variable can also be created and initialized with a string literal (constant). Ø String literals are groups of characters enclosed in quotation marks, as shown next. Ø char my. Str [] = “Test”; Ø Assigning a string literal to a character array, as the preceding code shows, creates the necessary number of memory elements—in this case five including the NULL character. 4

char my. Str[5] = {‘T’, ‘e’, ‘s’, ‘t’, ‘�’}; or char my. Str[] =

char my. Str[5] = {‘T’, ‘e’, ‘s’, ‘t’, ‘’}; or char my. Str[] = “Test”; T e s t my. Str[0] my. Str[1] my. Str[2] my. Str[3] my. Str[4] 5

char my. Str[5] = “is”; i s � ? my. Str[0] my. Str[1] my.

char my. Str[5] = “is”; i s ? my. Str[0] my. Str[1] my. Str[2] my. Str[3] ? my. Str[4] 6

The string constant “x” is not the same as the character constant ‘x’. Ø

The string constant “x” is not the same as the character constant ‘x’. Ø One difference is that ‘x’ is a basic type (char), but “x” is a derived type, an array of char. Ø A second difference is that “x” really consists of two characters, ‘x’ and ‘’, the null character Ø 7

char cx = ‘x’; char[] sx = “x”; x x � cx sx[0] sx[1]

char cx = ‘x’; char[] sx = “x”; x x cx sx[0] sx[1] 8

Ø You can access individual elements within a string the same way you access

Ø You can access individual elements within a string the same way you access elements in an array. char my. Str[] = “Test”; my. Str[0] = ‘B’; B e s t my. Str[0] my. Str[1] my. Str[2] my. Str[3] my. Str[4] 9

Ø Exercise 1: String 1. Start and prepare to run a new project in

Ø Exercise 1: String 1. Start and prepare to run a new project in VC++ Express 2. Copy the code from string. c and paste into the code editor. 3. Run the code and observe the result. 4. Let’s play with the code. 10

Ø You can print the entire string with a single printf() function, as follows:

Ø You can print the entire string with a single printf() function, as follows: printf(my. Str); or printf("%s", my. Str); 11

Ø You can also get the entire string with a scanf() function, as follows:

Ø You can also get the entire string with a scanf() function, as follows: scanf("%s", my. Str); Ø Ø Note that after scanf() starts to read input, it stops reading at the first whitespace (blank, tab, or newline) it encounters. In general, scanf() is used with %s to read only a single word, not a whole phrase, as a string. C has other input-reading functions, such as gets(), for handling general strings. 12

Ø Exercise 2: Get and display strings 1. Start and prepare to run a

Ø Exercise 2: Get and display strings 1. Start and prepare to run a new project in VC++ Express 2. Copy the code from name. c and paste into the code editor. 3. Run the code and observe the result. 4. Let’s play with the code. 13

Ø Because a character array can be used only to hold a string, it

Ø Because a character array can be used only to hold a string, it cannot go on the left side of an equal (=) sign. The program that follows is invalid: #include <stdio. h> main() { char petname[20]; petname = ”Duke”; printf(petname); return 0; } /* INVALID! */ 14

You can use the strcpy() function. This is a built-in function enabling you to

You can use the strcpy() function. This is a built-in function enabling you to copy a string constant into a string, or to copy a string to another string. Ø The strcpy() function assumes that the first value is a character array, and that the second value is a valid string constant or another character array that holds a string. Ø You must be sure that the first character array in the parentheses is long enough to hold whatever string you copy into it. 15 Ø

Ø Exercise 3: Copy a string 1. Start and prepare to run a new

Ø Exercise 3: Copy a string 1. Start and prepare to run a new project in VC++ Express 2. Copy the code from strcpy. c and paste into the code editor. 3. Run the code and observe the result. 4. Let’s play with the code. 16

A common practice among programmers is manipulating string data, such as copying one string

A common practice among programmers is manipulating string data, such as copying one string into another and concatenating (gluing) strings to each other. Ø Also common is the need to convert strings to either all lowercase or all uppercase, which can be important when comparing one string to another. Ø 17

Ø Ø The string length - strlen() - function is part of the string-handling

Ø Ø The string length - strlen() - function is part of the string-handling library <string. h>. strlen() takes a reference to a string and returns the numeric string length up to the NULL or terminating character, but not including the NULL character. 18

Ø Exercise 4: String length 1. Start and prepare to run a new project

Ø Exercise 4: String length 1. Start and prepare to run a new project in VC++ Express 2. Copy the code from strlen. c and paste into the code editor. 3. Run the code and observe the result. 4. Let’s play with the code. 19

An important reason for converting strings to either all uppercase or all lowercase is

An important reason for converting strings to either all uppercase or all lowercase is for string comparisons. Ø The character-handling library <ctype. h> provides many character manipulation functions such as tolower() and toupper(). These functions provide an easy way to convert a single character to either uppercase or lowercase. Ø To convert an entire string to either all uppercase or all lowercase, you will need to use a loop. 20 Ø

Ø Exercise 5: Upper and lower cases 1. Start and prepare to run a

Ø Exercise 5: Upper and lower cases 1. Start and prepare to run a new project in VC++ Express 2. Copy the code from upperlower. c and paste into the code editor. 3. Run the code and observe the result. 4. Let’s play with the code. 21

Another interesting and sometimes useful string library function is the strcat() function, which concatenates

Another interesting and sometimes useful string library function is the strcat() function, which concatenates or glues one string to another. Ø Like the strcpy() function, the strcat() function takes two string arguments. Ø 22

Ø Exercise 6: Concatenating strings 1. Start and prepare to run a new project

Ø Exercise 6: Concatenating strings 1. Start and prepare to run a new project in VC++ Express 2. Copy the code from strcat. c and paste into the code editor. 3. Run the code and observe the result. 4. Let’s play with the code. 23

The strcmp() function is primarily used to compare two strings for equality. Ø The

The strcmp() function is primarily used to compare two strings for equality. Ø The strcmp() function takes two strings as arguments and compares them using ASCII codes. Ø After comparing the two strings, the strcmp() function returns a single numeric value that indicates whether the first string is equal to, less than, or greater than the second string. 24 Ø

Sample Return value Description strcmp(string 1, string 2) 0 string 1 is equal to

Sample Return value Description strcmp(string 1, string 2) 0 string 1 is equal to string 2 strcmp(string 1, string 2) <0 string 1 is less than string 2 strcmp(string 1, string 2) >0 string 1 is greater than string 2 25

Ø Exercise 7: Comparing strings 1. Start and prepare to run a new project

Ø Exercise 7: Comparing strings 1. Start and prepare to run a new project in VC++ Express 2. Copy the code from strcmp 1. c and paste into the code editor. 3. Run the code and observe the result. 4. Let’s play with the code. 26

Ø Exercise 8: Comparing strings 2 1. Start and prepare to run a new

Ø Exercise 8: Comparing strings 2 1. Start and prepare to run a new project in VC++ Express 2. Copy the code from strcmp 2. c and paste into the code editor. 3. Run the code and observe the result. 4. Let’s play with the code. 27

The gets() (get string) function gets a string from your system's standard input device,

The gets() (get string) function gets a string from your system's standard input device, normally your keyboard. Ø Its method is to read characters until it reaches a newline (n) character, which you generate by pressing the Enter key. Ø It takes all the characters up to (but not including) the newline, tacks on a null character (), and gives the string to the calling program. Ø 28

The puts() function is very easy to use. Just give it the address (name)

The puts() function is very easy to use. Just give it the address (name) of a string for an argument. Ø Unlike printf(), puts() automatically appends a newline when it displays a string. Ø 29

Ø Exercise 9: gets() and puts() 1. Start and prepare to run a new

Ø Exercise 9: gets() and puts() 1. Start and prepare to run a new project in VC++ Express 2. Copy the code from getsputs. c and paste into the code editor. 3. Run the code and observe the result. 4. Let’s play with the code. 30

Because one string is an array of characters, an array of strings is a

Because one string is an array of characters, an array of strings is a 2 D array of characters in which each row is one string. Ø The following are statements to declare an array to store up to 30 names, each of which is less than 50 characters long. Ø char names[30][50]; 31

Ø Exercise 10: Array of strings 1. Start and prepare to run a new

Ø Exercise 10: Array of strings 1. Start and prepare to run a new project in VC++ Express 2. Copy the code from stringarray. c and paste into the code editor. 3. Run the code and observe the result. 4. Let’s play with the code. 32