Lecture 19: Working with strings What Is a String? Importance Used in developing editors, word processors, page layout software, any kinds of text-processing software Strings Series.

Download Report

Transcript Lecture 19: Working with strings What Is a String? Importance Used in developing editors, word processors, page layout software, any kinds of text-processing software Strings Series.

Lecture 19: Working with
strings
What Is a String?
Importance
Used in developing editors, word processors, page
layout software, any kinds of text-processing software
Strings
Series of characters treated as a single unit.
Can include letters, digits and various special
characters such as +, -, *, /, $, \t, \n.
String literal (string constant) in C are written in double
quotation marks.
“Hello EPSII”
In C, a string is an array of characters ending in the
null character (‘\0’).
Definition and Initialization
Similar to the array definition; The data type of the
elements is char.
Strings are represented as character arrays ending
with '\0'
Strintgs
char str[ ] = “hello”;
char str[ ] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
Strintgs (#define SIZE 10)
char str[SIZE] = “hello”;
• SIZE > length(“hello”)
Arrays
int aryInt[ ] = {1, 2, 3, 4, 5};
char aryChar[ ] = {‘h’, ‘e’, ‘l’, ‘l’, ’o’};
Arrays (#define SIZE 10)
int aryInt[SIZE] = {1, 2, 3, 4, 5};
char aryChar[SIZE] = {‘E’, ‘P’, ‘S’};
The value of a string is the address of its first character.
A string is a pointer to the string’s first character.
An array is also a constant pointer to its first element.
Definition and Initialization
A string can be defined as a variable of type char *
const char *colorPtr = “blue”;
Create a pointer variable colorPtr that points to the
string “blue” somewhere in memory.
Inputting strings
Using scanf
scanf(“%s”, word);
Copies input into word[ ]
Do not need & (a string evaluates the address of
its first character)
Remember to leave room in the array for ‘\0’
char word[20];
scanf(“%19s”, word);
scanf reads up to 19 characters
Standard Input/Output Library Functions
Functions in <stdio.h>
Used to manipulate character and string data
Standard Input/Output Library Functions
Function prototype
Function de scription
i nt g etch ar( void );
Inputs the next character from the standard input and
returns it as an integer.
cha r *get s( char *s );
Inputs characters from the standard input into the array
s until a newline or end-of-file character is encountered.
A terminating null character is appended to the array.
Returns the string inputted intos. Note that an error will
occur if s is not large enough to hold th
e string.
i nt putchar( i nt c );
Prints the character stored in c and returns it as an integer.
i nt puts( con st char *s );
Prints the strings followed by a newline character. Returns
a non-zero integer if successful , or EOF if an error occurs.
i nt sprin tf( char *s, con st char *form at, ... );
Equivalent to printf, except the output is store
d in
the array s instead of printed on the screen. Returns
the number of characters written to s, or EOF if an
error occurs.
i nt sscanf( char *s, con st ch ar *format, ... );
Equivalent to scanf, except the input is read from
the array s rather than from the keyboard. Returns the
number of items successfully read by the function, or
EOF if an error occurs.
String Manipulation Functions of the
String Handling Library
Functions in <string.h>
String handling library has functions to
– Manipulate string data
– Search strings
– Tokenize strings
– Determine string length
String Manipulation Functions of the
String Handling Library
Function prototype
Function description
char *strcpy( char *s1, const char *s2 )
Copies strings2 into array s1. T he value of s1 is returned.
char *strncpy( char *s1, const char *s2, size_t n )
Copies at most n characters of string s2 into array s1. The value of s1
is returned.
char *strcat( char *s1, const char *s2 )
Appends string s2 to array s1. T he first character of s2 overwrites the
terminating null character of s1. T he value of s1 is returned.
char *strncat( char *s1, const char *s2, size_t n )
Appends at most n characters of string s2 to array s1. The first
character of s2 overwrites the terminating null character of s1.
T he value of s1 is returned.
String Manipulation Functions of the
String Handling Library
Function prototype Function description
int strcmp( const char *s1, const char *s2 );
Compares the string s1 with the string s2. The fu nction returns
0, less than 0 or greater than 0 if s1 is equal to, less than or
greater than s2, respectively.
int strncmp( const char *s1, const char *s2, size_t n );
Compares up to n characters of the string s1 with the string s2.
T he function returns 0, less than 0 or greater than 0 if s1 is
equal to, less than or greater than s2, respectively.
Function prototype
Function description
size_t strlen( const char *s );
Determines the length of string s. T he number of characters
preceding the terminating null character is returned.
Practice Question
Q. What is NOT the right way to create a character array and assign it the string “I love
EPS II”?
A.
char string[25]= “I love EPS II”;
B.
char string[25];
strcpy(string, “I love EPS II”;
C.
char string[25];
string = “I love EPS II”;
D.
char string[25] = {‘I’, ‘ ‘, ‘l’, ‘o’, ‘v’, ‘e’, ‘ ‘,
‘E’, ‘P’, ‘S’, ‘ ‘, ‘I’, ‘I’, ‘\0’};
E.
char string[25];
sprintf(string, “%s”, “I love EPS II”);
Solution: C