0% found this document useful (0 votes)
10 views21 pages

L2 Strings

The document discusses strings in C programming including declaring and initializing strings, storing and representing string data, inputting strings using scanf and gets functions, common string manipulation functions from the string.h library like strlen, strcpy, strcmp, strcat, strstr, and some user-defined string functions for trimming strings. It also briefly covers arrays of strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views21 pages

L2 Strings

The document discusses strings in C programming including declaring and initializing strings, storing and representing string data, inputting strings using scanf and gets functions, common string manipulation functions from the string.h library like strlen, strcpy, strcmp, strcat, strstr, and some user-defined string functions for trimming strings. It also briefly covers arrays of strings.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

CHUỖI KÝ TỰ - STRINGS

Võ Quang Hoàng Khang


Declare/ Initialize a String
• Static strings: stored in data segment or stack
segment
• char s1[21]; /* for a string of 20 characters*/
Initialize a string: NULL byte is automatically
inserted.
char name[31] = “I am a student”;
char name2[31] = {‘H’, ‘e ‘, ‘l’, ‘l’, ‘o’, ‘\0’ };
• Dynamic strings: Stored in the heap
char* S;
S = (char*) malloc( lengthOfString+1);
S = (char*) calloc( lengthOfString+1, sizeof(char));

Strings 2
Data Stored in a strings
• Each character in a string is stored as it’s ASCII code.

S1[i]: The character at


the position i in the
string S1

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]; Default


scanf("%s", name );
Enter: My name is Arnold

char name[31];
scanf(“%10s", name );
Enter: Schwartzenegger

Strings 7
Input Strings: scanf(…)

How to accept blanks in a input string?


%[^\n] conversion specifier

• reads all characters until the newline ('\n'),


• stores the characters read in memory locations
starting with the address passed to scanf,
• stores the null byte in the byte following that where
scanf stored the last character and
• leaves the delimiting character (here, '\n') in the input
buffer.

Strings 8
Input Strings: scanf(…)

How to accept blanks in a input string?


%[^\n] conversion specifier.

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

%[A-Z] Catches all uppercase letters


%[0-9A-Za-z] Catches all decimal digits and all letters
%[A-FT-Z] Catches all uppercase letters from A to F and from T to Z

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(…)

2293612 n1: 10 Overflow


2293608 n2: 33

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

HOA ANH DAOhoa A


2293584 2293596

strstr()  NULL if the substring doesn’t exist.

Strings 14
Some User-Defined String Functions

Purpose Prototype
Trim blanks at the beginning of a char* lTrim(char s[])
string: “ Hello”  “Hello”

Trim blanks at the end of a char* rTrim(char s[])


string: “Hello ”  “Hello”

Trim extra blanks in a string: char* trim (char s[])


“ I am a student “
 “I am a student”
Convert a string to a name: char* nameStr( char s[])
“ hoang thi hoa “
 “Hoang Thi Hoa”

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

“Hoa Anh Dao No“

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

You might also like