Chapter 8 Strings Strings C implements the string
- Slides: 32
Chapter 8 Strings
Strings • C implements the string data structure using arrays of type char. • Since string is an array, the declaration of a string is the same as declaring a char array. – char string_var[30]; – char string_var[20] = “Initial value”; Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -2
Memory Storage for a String • The string is always ended with a null character ‘ ’ • The characters after the null character are ignored. • e. g. , char str[20] = “Initial value”; [0] I n i t i a l Copyright © 2004 Pearson Addison-Wesley. All rights reserved. v a l u [13] e ? ? … 9 -3
Input/Output of a String • The placeholder %s is used to represent string arguments in printf and scanf. – printf(“Topic: %sn”, string_var); • The string can be right-justified by placing a positive number in the placeholder. – printf(“%8 s”, %8 s str); • The string can be left-justified by placing a negative number in the placeholder. – Printf(“%-8 s”, %-8 s str); Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -4
Right and Left Justification of Strings The “%8 s” %8 s placeholder displays a string which is rightjustified and in 8 -columns width. If the actual string is longer than the width, the displayed field is expanded with no padding. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -5
An Example of Manipulating String with scanf and printf The dept is the initial memory address of the string argument. Thus we don’t apply the & operator on it. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -6
Execution of scanf ("%s", dept); • Whenever encountering a white space, the scanning stops and scanf places the null character at the end of the string. • e. g. , if the user types “MATH 1234 TR 1800, ” the string “MATH” along with ‘ 0’ is stored into dept. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -7
String Library Functions • The string can not be copied by the assignment operator ‘=’. – e. . g, “str = “Test String”” is not valid. • C provides string manipulating functions in the “string. h” library. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -8
Some String Functions from String. h Function Purpose strcpy Makes a copy of a string Appends a string to the end of another string Compare two strings alphabetically Returns the number of characters in a string Breaks a string into tokens by delimiters. strcat strcmp strlen strtok Copyright © 2004 Pearson Addison-Wesley. All rights reserved. Example strcpy(s 1, “Hi”); strcat(s 1, “more”); strcmp(s 1, “Hu”); strlen(“Hi”) returns 2. strtok(“Hi, Chao”, “ , ”); 9 -9
Functions strcpy and strncpy • Function strcpy copies the string in the second argument into the first argument. – e. g. , strcpy(dest, “test string”); – The null character is appended at the end automatically. – If source string is longer than the destination string, the overflow characters may occupy the memory space used by other variables. • Function strncpy copies the string by specifying the number of characters to copy. – You have to place the null character manually. – e. g. , strncpy(dest, “test string”, 6); dest[6] = ‘ ’; – If source string is longer than the destination string, the overflow characters are discarded automatically. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -10
String Assignment
Extracting Substring of a String (1/2) • We can use strncpy to extract substring of one string. – e. g. , strncpy(result, s 1, 9); Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -12
Extracting Substring of a String (2/2) • e. g. , strncpy(result, &s 1[5] 2); Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -13
Figure 8. 7 Program Using strncpy and strcpy Functions to Separate Compounds into Elemental Components 1 -14
Functions strcat and strlen • Functions strcat and strncat concatenate the fist string argument with the second string argument. – strcat(dest, “more. . ”); – strncat(dest, “more. . ”, 3); • Function strlen is often used to check the length of a string (the number of characters before the fist null character). Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -15
String Concatenation • Functions strcat and strncat concatenate the fist string argument with the second string argument. char f 1[15] = "John"; char f 2[15] = "Jacqueline"; char f 3[15] = "Bob"; char last[15] = "Kennedy"; strcat(f 1, last); puts(f 1); strcat(f 2, last); /* invalid overflow */ puts(f 2); strncat(f 3, last, 5); puts(f 3);
Distinction Between Characters and Strings • The representation of a char (e. g. , ‘Q’) and a string (e. g. , “Q”) is essentially different. – A string is an array of characters ended with the null character. Q Character ‘Q’ Copyright © 2004 Pearson Addison-Wesley. All rights reserved. Q String “Q” 9 -17
String Comparison (1/2) • Suppose there are two strings, str 1 and str 2. – The condition compare the initial memory address of str 1 and of str 2. • The comparison between two strings is done by comparing each corresponding character in them. – The characters are comapared against the ASCII table. – “thrill” < “throw” since ‘i’ < ‘o’; – “joy” < joyous“; • The standard string comparison uses the strcmp and strncmp functions. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -18
String Comparison (2/2) Relationship Returned Value Example str 1 < str 2 Negative “Hello”< “Hi” str 1 = str 2 0 “Hi” = “Hi” str 1 > str 2 Positive “Hi” > “Hello” • e. g. , we can check if two strings are the same by if(strcmp(str 1, str 2) != 0) printf(“The two strings are different!”); Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -19
strcmp example #include <stdio. h> #include <stdlib. h> int main() { char s 1[10] = "SAM", s 2[10]="SAM" ; int len; len = strcmp (s 1, s 2); if (len == 0) printf ("Two Strings are Equal"); return 0; } Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -20
Input/Output of Characters and Strings • The stdio library provides getchar function which gets the next character from the standard input. – “ch = getchar(); ” is the same as “scanf(“%c”, &ch); ” – Similar functions are putchar, gets, puts. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -21
getchar Example int main( ) { char ch; ch = getchar(); printf("Accepted Character : %c", ch); return 0; } Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -22
Character Analysis and Conversion • The <ctype. h> library defines facilities for character analysis and conversion. Functions Description isalpha Check if the argument is a letter isdigit Check if the argument is one of the ten digits isspace Check if argument is a space, newline or tab. tolower Converts the uppercase letters in the argument to lowercase letters. toupper Converts the lowercase letters in the argument to uppercase letters. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -23
#include <stdio. h> #include <ctype. h> int main() { char var 1 = 'd'; char var 2 = 't'; if( isalpha(var 1) ) { printf("var 1 is an alphabet lettern", var 1 ); } else{ printf("var 1 is not an alphabet lettern", var 1 ); } if( isalpha(var 2) ){ printf("var 2 is an alphabet lettern", var 2 ); } else{ printf("var 2 is not an alphabet lettern", var 2 ); } return(0); } isalpha Example Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -24
isdigit Example int main() { char ch = '1'; if( isdigit (ch)) printf("n. This is Digit"); else printf("n. This is not a Digit"); return 0; } Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -25
isspace Example int main() { char var = ' '; if( isspace(var) ) { printf("it is a white-space charactern", var ); } else { printf ("it is not a white-space character", var ); } return 0; } Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -26
#include <stdio. h> #include <ctype. h> tolower Example int main() { int i = 0; char c; char str[] = "MALTEPE"; while( str[i] ) { putchar(tolower(str[i])); i++; } return(0); } Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -27
Conversions Between Strings Numbers • The <stdlib. h> defines some basic functions for conversion from strings to numbers: – atoi(“ 123”) converts a string to an integer. – atol(“ 123”) converts a string to a long integer. – atof(“ 12. 3”) converts a string to a float. • However, there is no functions such as itoa, itof, …etc, – because there is a function called sprintf which can converts many formats to a string. Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -28
#include <stdio. h> #include <string. h> int main() { int val; char str 1[3]="44"; char str 2[3]="66"; val = atoi(str 1)+atoi(str 2); printf("Total = %dn", val); return(0); } Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -29
strtok example int main() { char string[] = "a string, of , , tokens"; char *token; token = strtok(string, " , "); /*There are two delimiters here*/ while (token != NULL){ printf("The token is: %sn", token); token = strtok(NULL, " , "); } return 0; } Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -30
strtok example Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -31
Arrays of Strings • An array of strings is a two-dimensional array of characters in which each row is one string. – char month[5][10] = {“January”, “February”, “March”, “April”, “May”}; Copyright © 2004 Pearson Addison-Wesley. All rights reserved. 9 -32
- Http protocol description
- Const int size=18; string *tbl2 = new string[size];
- Public string name
- New string[1]
- Thread class hierarchy in java
- Sqlite is an in-process library that implements a
- Difference between primary and secondary tillage implements
- Mimi opkins
- Most quats solutions disinfect implements in
- Implements comparable
- Java uml interface
- The event table contains these columns
- Extends jframe implements actionlistener
- A manual calculator implements algorithms autonomously.
- Chapter 25 manicuring milady
- Extends jframe implements actionlistener
- Hình ảnh bộ gõ cơ thể búng tay
- Lp html
- Bổ thể
- Tỉ lệ cơ thể trẻ em
- Voi kéo gỗ như thế nào
- Glasgow thang điểm
- Chúa yêu trần thế
- Môn thể thao bắt đầu bằng chữ f
- Thế nào là hệ số cao nhất
- Các châu lục và đại dương trên thế giới
- Cong thức tính động năng
- Trời xanh đây là của chúng ta thể thơ
- Cách giải mật thư tọa độ
- Làm thế nào để 102-1=99
- Phản ứng thế ankan
- Các châu lục và đại dương trên thế giới
- Thơ thất ngôn tứ tuyệt đường luật