Chapter 7
CS12 - Computer Programming 1
Chapter 7
1
Objectives Understand the concept of string variables in C programs Learn how to incorporate string variables in C programs Know how to manipulate string variables such as character conversion, formatting, and validation check. Know and appreciate when should string variables be included and the purpose of using string variables CS12 - Computer Programming 1
Chapter 7
2
String Variables a list of characters declared as array Example – Declaration: char Name[10]; Example – Initialization: char Name[10] = { ‘P’, ‘E’, ‘T’, ‘E’, ‘R’}; char Name[10] = “Peter”; char Name[ ] = “Peter”; Once a string variable is declared, characters could be stored in the allocated storage space Each character would be accessed/referenced via index Chapter 7 3
CS12 - Computer Programming 1
String Output Printf Puts %s - outputs an entire string printf (“%s”, Name); printf(“%20s”, Name); printf(“%-20s”, Name); /* left justification */ printf(“%20.10”, Name); /* width = 20, no. of characters to print = 10 OR puts(Name); CS12 - Computer Programming 1
Chapter 7
4
String Input A string variable could be used to store any ASCII characters Two ways of capturing value into string variable: scanf( ) function - stop conversion at the first whitespace or a maximum of 9 characters gets( ) function - does not limit the number of characters assigned to the string
Example: char product[9]; scanf(“%9s”, product); CS12 - Computer Programming 1
Chapter 7
5
String Manipulation Commonly used: Available in the string.h header strlen() – it returns the number of characters in the string. char Name[40] = ”Peter”; printf (“String size is: %i”, strlen(Name));
strcpy() - copies a string from location to another char Destination[40]; char Source[40] = “Peter”; strcpy(Destination, Source); CS12 - Computer Programming 1
Chapter 7
6
String Manipulation strlen() – it returns the number of characters in the string. char Name[40] = ”Peter”; printf (“String size is: %i”, strlen(Name));
strcpy() - copies a string from location to another char Destination[40]; char Source[40] = “Peter”; strcpy(Destination, Source);
strcat( ) – concatenates strings, puts one string at the end of another char First[40] = “Peter”; char Second[40] = “Pan”; printf(“%s”, strcat(First, Second)); CS12 - Computer Programming 1
Chapter 7
7
String Manipulation strcmp( ) – compares two strings and return a positive value if the first string is greater than the second string, zero if both strings are equal, or negative value if first string is less than second string. char First[40] = “Peter”; char Second[40] = “Pan”; if( strcmp(First, Second) == 0) printf(“They are equal”);
CS12 - Computer Programming 1
Chapter 7
8
String Manipulation strncmp( ) – compares only up to maximum characters specified. char First[40] = “Peter”; char Second[40] = “Pan”; if( strncmp(First, Second, 3) == 0) printf(“The first three characters are equal”);
CS12 - Computer Programming 1
Chapter 7
9
Character Classification Various character classification functions available in ctype.h header file: • Isalnum() – returns nonzero if the character is alphanumeric: 0-9, A-Z, or a-z • isalpha() – returns nonzero if the character is alphabetic: A-Z or a-z • iscntrl() – returns nonzero if the character is a control code: ASCII 1-31 • Isdigit() – returns nonzero if the character is a decimal digit: 0-9 • Isgraph() – returns nonzero if the character is printable, not including space • islower() – returns nonzero if the character is lowercase: a-z CS12 - Computer Programming 1
Chapter 7
10
Character Classification • isprintf( ) - returns nonzero if the character is printable. Including space. • ispunct( ) - returns nonzero if the character is punctuation. • isspace( ) - returns nonzero if the character is white space: space, form feed (\f), newline (\n), return (\r), horizontal tab (\t), or vertical tab (\v). • isupper( ) - returns nonzero if the character is upper case: A-Z. • isxdigit( ) - returns nonzero if the character is a hexadecimal digit: 0-9, A-F. CS12 - Computer Programming 1
Chapter 7
11