chapter 8 strings. copyright ©2004 pearson addison-wesley. all rights reserved.9-2 strings stringc...
TRANSCRIPT
![Page 1: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/1.jpg)
Chapter 8Strings
![Page 2: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/2.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-2
Strings
• C implements the stringstring data structure using arrays of type char.
• Since stringstring 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”;
![Page 3: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/3.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-3
Memory Storage for a String
• The string is always ended with a null null charactercharacter ‘\0’‘\0’.
• The characters after the null character are ignored.
• e.g., char str[20] = “Initial value”;
n i t i a l v a l u e ? ? …I \0\0
[0] [13]
![Page 4: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/4.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-4
Input/Output of a String
• The placeholder %s%s is used to represent string arguments in printf and scanf.– printf(“Topic: %s\n”, string_var);
• The string can be right-justified by placing a positive number in the placeholder.– printf(“%8s%8s”, str);
• The string can be left-justified by placing a negative number in the placeholder.– Printf(“%-8s%-8s”, str);
![Page 5: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/5.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-5
Right and Left Justification of Strings
The “%8s%8s” placeholder displays a string which is right-justified and in 8-columns width.If the actual string is longer than the width, the displayed field is expanded with no padding.
![Page 6: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/6.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-6
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.
![Page 7: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/7.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-7
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.
![Page 8: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/8.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-8
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.
![Page 9: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/9.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-9
Some String Functions from String.h
Function Purpose Example
strcpy Makes a copy of a string
strcpy(s1, “Hi”);
strcat Appends a string to the end of another string
strcat(s1, “more”);
strcmp Compare two strings alphabetically
strcmp(s1, “Hu”);
strlen Returns the number of characters in a string
strlen(“Hi”) returns 2.
strtok Breaks a string into tokens by delimiters.
strtok(“Hi, Chao”, “ ,”);
![Page 10: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/10.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-10
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 characternull 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] = ‘\0’;dest[6] = ‘\0’;– If source string is longer than the destination string, the
overflow characters are discarded automatically.
![Page 11: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/11.jpg)
String Assignment
![Page 12: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/12.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-12
Extracting Substring of a String (1/2)
• We can use strncpy to extract substring of one string.– e.g., strncpy(result, s1, 9);
![Page 13: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/13.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-13
Extracting Substring of a String (2/2)
• e.g., strncpy(result, &s1[5]&s1[5], 2);
![Page 14: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/14.jpg)
1-14
Figure 8.7 Program Using strncpy and strcpy Functions to Separate Compounds intoElemental Components
![Page 15: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/15.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-15
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).
![Page 16: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/16.jpg)
String Concatenation• Functions strcat and strncat concatenate the
fist string argument with the second string argument.
char f1[15] = "John"; char f2[15] = "Jacqueline"; char f3[15] = "Bob"; char last[15] = "Kennedy"; strcat(f1, last); puts(f1); strcat(f2, last); /* invalid overflow */ puts(f2); strncat(f3, last, 5); puts(f3);
![Page 17: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/17.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-17
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’
Q \0\0
String “Q”
![Page 18: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/18.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-18
String Comparison (1/2)
• Suppose there are two strings, str1 and str2. – The condition compare the initial memory addressinitial memory address of str1
and of str2.
• 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.
![Page 19: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/19.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-19
String Comparison (2/2)
Relationship Returned Value Example
str1 < str2 Negative “Hello”< “Hi”
str1 = str2 0 “Hi” = “Hi”
str1 > str2 Positive “Hi” > “Hello”
• e.g., we can check if two strings are the same byif(strcmp(str1, str2) != 0)printf(“The two strings are different!”);
![Page 20: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/20.jpg)
strcmp example
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-20
#include <stdio.h>#include <stdlib.h>
int main(){char s1[10] = "SAM", s2[10]="SAM" ;int len;len = strcmp (s1,s2);
if (len == 0) printf ("Two Strings are Equal");
return 0;}
![Page 21: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/21.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-21
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.
![Page 22: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/22.jpg)
getchar Example
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-22
int main( ) { char ch;
ch = getchar();
printf("Accepted Character : %c",ch);
return 0;
}
![Page 23: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/23.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-23
Character Analysis and Conversion• The <ctype.h><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.
![Page 24: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/24.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-24
#include <stdio.h>#include <ctype.h>int main(){ char var1 = 'd'; char var2 = '\t'; if( isalpha(var1) ) { printf("var1 is an alphabet letter\n", var1 ); } else{ printf("var1 is not an alphabet letter\n", var1 ); } if( isalpha(var2) ){ printf("var2 is an alphabet letter\n", var2 ); } else{ printf("var2 is not an alphabet letter\n", var2 ); } return(0);}
isalpha Example
![Page 25: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/25.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-25
int main(){
char ch = '1';
if( isdigit (ch)) printf("\nThis is Digit");else printf("\nThis is not a Digit");return 0;}
isdigit Example
![Page 26: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/26.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-26
int main(){ char var = ' '; if( isspace(var) ) { printf("it is a white-space character\n", var ); } else { printf ("it is not a white-space character",var ); }
return 0;}
isspace Example
![Page 27: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/27.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-27
#include <stdio.h>#include <ctype.h>
int main(){ int i = 0; char c; char str[] = "MALTEPE";
while( str[i] ) { putchar(tolower(str[i])); i++; } return(0);}
tolower Example
![Page 28: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/28.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-28
Conversions Between Strings Numbers• The <stdlib.h> defines some basic functions for
conversion from strings to numbers:– atoi(“123”)atoi(“123”) converts a string to an integer.– atol(“123”)atol(“123”) converts a string to a long integer.– atof(“12.3”)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 sprintfsprintf which can
converts many formats to a string.
![Page 29: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/29.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-29
#include <stdio.h>#include <string.h>
int main(){ int val; char str1[3]="44"; char str2[3]="66";
val = atoi(str1)+atoi(str2); printf("Total = %d\n", val); return(0);}
![Page 30: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/30.jpg)
strtok example
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-30
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: %s\n", token); token = strtok(NULL," ,"); }return 0;}
![Page 31: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/31.jpg)
strtok example
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-31
![Page 32: Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of](https://reader036.vdocuments.site/reader036/viewer/2022062423/5697bf821a28abf838c859d4/html5/thumbnails/32.jpg)
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 9-32
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”};