L2 Strings
L2 Strings
Strings 2
Data Stored in a strings
• Each character in a string is stored as it’s ASCII code.
Strings 3
Output Strings – Test yourself
Observe the
prompt symbol
on the result
screen .
Strings 4
Input Strings
• Library: stdio.h
• Function scanf() with type conversion %s
• Function gets(string)
• Each function has it’s own advantages and
weaknesses.
Strings 5
Input Strings: scanf(…)
The %s conversion specifier
• reads all characters until the first whitespace
character,
• stores the characters read in memory locations
starting with the address passed to scanf,
• Automatically stores the null byte in the memory
byte following the last character accepted and
• leaves the delimiting whitespace plus any
subsequent characters in the input buffer ignores
any leading whitespace characters (default).
• Option specifiers are used to change default
characteristics of the function scanf on strings.
Strings 6
Input Strings: scanf(…)
char name[31];
scanf(“%10s", name );
Enter: Schwartzenegger
Strings 7
Input Strings: scanf(…)
Strings 8
Input Strings: scanf(…)
Strings 9
Input Strings: scanf(…)
Some character specifiers used in the function
scanf(): Set of character are or not accepted.
Specifier Description
%[abcd] Searches the input field for any of the characters a, b, c, and d
%[^abcd] Searches the input field for any characters except a, b, c, and d
%[0-9] To catch all decimal digits
Strings 10
Input Strings: gets(…)
gets is a standard library function (stdio.h) that
– accepts an empty string
– uses the '\n' as the delimiter
– throws away the delimiter after accepting the string
– Automatically appends the null byte to the end of the
set stored
The prototype for gets is
char* gets(char [ ]);
(gets is dangerous. It can fill beyond the
memory that allocated for the string)
Strings 11
Input Strings: gets(…)
s
2293584
2293580 12
Strings 12
Others String Functions: string.h
Purpose Function
Get the length of a string int strlen (char s[])
Copy source string to destination string char* strcpy(char dest[], char src[])
Compare two strings int strcmp( char s1[], char s2[]) -1, 0, 1
Concatenate string src to the end of dest char* strcat(char dest[], char src[])
Convert a string to uppercase char* strupr(char s[])
Convert a string to lowercase char* strlwr(char s[])
Find the address of a substring char* strstr (char src[], char subStr[])
NULL if subStr does not exist in the src.
Strings 13
Others String Functions: string.h
Strings 14
Some User-Defined String Functions
Purpose Prototype
Trim blanks at the beginning of a char* lTrim(char s[])
string: “ Hello” “Hello”
Strings 15
Some user-defined String Functions
0 1 2 3 4 5 6
H o a NULL
i=0 1 2 3
0 1 2 3 4 5 6
H o a NULL o a NULL
Strings 16
Some user-defined String Functions
0 1 2 3 4 5 6
H o a NULL
2 3 4 i=5
0 1 2 3 4 5 6
H o a NULL NULL
Strings 17
Some user-defined String Functions
“ Hoa anh dao “ “Hoa anh dao“
“Hoa anh dao“
lTrim
“Hoa anh dao“
“Hoa anh dao “ “Hoa anh dao“
rTrim “Hoa anh dao“
“Hoa anh dao“ “Hoa anh dao“
Strings 18
Some user-defined String Functions
“ hOA anH dAo nO “
trim()
“hOA anH dAo nO“ h o a a n h d a o n o
strlwr() 0 1 2 3 4 5 6 7 8 9 1 1 1 1
“hoa anh dao no“ 0 1 2 3
Strings 19
Some user-defined String Functions
Strings 20
Summary
String Input
– scanf
– gets
– Do yourself using getchar()
String Functions and Arrays of Strings
• Functions
– strlen
– strcpy
– strcmp
– strcat Q&A
– strstr
• Arrays of Strings
– Input and Output
– Passing to Functions
– Sorting an Array of Names
Strings 21