String

Report
C/C++ Training
Programming C/C++ on Eclipe
Trình bày : Ths HungNM
String










Introduction String.
String variable.
Reading and writing string
Accessing character in a string
Using the string library
String idioms
Arrays of strings.
Analyzing and Transforming Strings
Converting Strings to Numerical Values
Working with Wide Character Strings
EcoSoftware
Training C/C++
2
Introduction String

Introduction string (literal string)
A string constant is a sequence of characters.
 Or symbols between a pair of double-quote characters.

• Example : printf("This is a string.");

How string literal are stored.
String literal as a character arrays.
 Example :

• “This is a string”
EcoSoftware
Training C/C++
3
String variable

Initializing a string variable.

String variable can be initialized at the same time.
• char date1[8] = “June 14”;
• char date1[0] = {‘J’,’u’,’n’,’e’,’ ‘,’1’,’4’,’\0’};
J

u
n
e
1
4
\0
Character array versus character pointer.

Character array :
• char date[] = “June 14”;

Character pointer:
• char *date = “June 14”;
EcoSoftware
Training C/C++
4
Reading and writing string

Writing string with printf and puts.
The %s conversion specification allow printf to write
string.
 Example :

• char str[] = “Are you having fun yet ?”;
• printf(“%s\n”,str);

The C library provides puts function.
• puts(str);
EcoSoftware
Training C/C++
5
Reading and writing string

Reading strings using scanf and gets.





scanf to read a string into array chars.
It skips white space.
Always store null character at the and of the string.
Syntax : scanf(“%s”,str);
The C library provides gets function.




The gets() function is the simplest method of accepting a string
through standard input.
Input characters are accepted till the Enter key is pressed.
The gets() function replaces the terminating ‘\n’ new line character
with the ‘\0’ character.
Syntax : gets(str);
EcoSoftware
Training C/C++
6
Reading and writing string

Reading strings character by character.

Using a loop and getchar function to read a character
and the store character in array.
EcoSoftware
Training C/C++
7
Accessing character in a string

Using loop to accessing
character in a string.

Example.
Using array index
EcoSoftware
Using pointer
Training C/C++
8
Using C String library

The C library provides a rich set of function for performing
operation on strings.
Function name
strcpy(str1, str2);
strlen(str1);
strcat(str1, str2);
strcmp(str1, str2);

EcoSoftware
Test for
String copy function
Length of string
Joining strings function
Comparing string function

Training C/C++
9
String copy function

The strcpy (string copy function).
Copies the value in one string onto another.
 The value of str2 is copied onto str1
 The return str1.
 Syntax :

• strcpy(str1, str2);
• char *strcpy(char *str1,const char *str2);

Exampe :
• char *str2, *str1 = NULL;
• strcpy(str2, “abcde”); // str2 has content “abcde”.
• strcpy(str1,str2); // str1 has content “abcde”
EcoSoftware
Training C/C++
10
String length function

Using strlen() to caculate length of a string.
Syntax :size_t strlen(const char *str1);
 Example :

• int len = 0;
• len = strlen(“abc”); // len is now 3;
• len = strlen(“”); // len is now 0;
EcoSoftware
Training C/C++
11
Joining Strings Using a Library Function


Joins two string values into one.
Syntax :


char *strcat(char *str1,const char *str2);
Example :
strcpy(str1,”abc”); // str1 has content “abc”.
 strcat(str1,”def”); // now str1 has content “abcdef”

EcoSoftware
Training C/C++
12
Comparing string function


Compares two strings and returns an integer value based
on the results of the comparison.
The function returns a value:
Less than zero if str1<str2
Zero if str1 is same as str2
Greater than zero if str1>str2




Syntax :


int strcmp(char *str1, const char *str2);
Example :



char str1[] = "The quick brown fox";
char str2[] = "The quick black fox";
int valuecmp = strcmp(str1,str2);
EcoSoftware
Training C/C++
13
String idioms

Idioms by using theme to write strlen and strcat
functions.

Search for ending of a string (strlen).
EcoSoftware
Training C/C++
14
String idioms

Copy a string (strcat function).
EcoSoftware
Training C/C++
15
Arrays of strings



Store the strings in the two-dimensional arrays.
Example :
 char planets[][8]={“Mercury”,”Venus”};
Image of strings in the two-dimensional arrays
EcoSoftware
M
e
r
c
u
r
y
\0
V
e
n
u
s
\0
\0
\0
Training C/C++
16
Arrays of strings

Using pointer:



Example :
char *planets[]={“Mercury”,”Venus”};
Images:
planets
0
1
EcoSoftware
M
V
e
e
r
n
c
u
u r y \0
s \0 \0 \0
Training C/C++
17
Analyzing and Transforming Strings

The C library ctype.h have many Character Classification Functions.
Function name
islower()
isupper()
isalpha()
isalnum()
iscntrl()
isprint()
isgraph()
isdigit()
isxdigit()
isblank()
isspace()
ispunct()

EcoSoftware
Test for
Lowercase letter
Uppercase letter
Uppercase or lowercase letter
Uppercase or lowercase letter or a digit
Control character
Any printing character including space
Any printing character except space
Decimal digit ('0' to '9')
Hexadecimal digit ('0' to '9', 'A' to 'F', 'a' to 'f')
Standard blank characters (space, '\t')
Whitespace character (space, '\n', '\t', '\v', '\r', '\f')
Printing character for which isspace() and isalnum() return
false

Training C/C++
18
Analyzing and Transforming Strings

Example :
EcoSoftware
Training C/C++
19
Converting Strings to Numerical Values

The <stdlib.h> header file declares functions that you can
use to convert a string to a numerical value.
Functions
Returns
atof()
A value of type double that is produced from
the string argument
atoi()
A value of type int that is produced from the
string argument.
atol()
A value of type long that is produced from the
string argument.
atoll()
A value of type long long that is produced from
the string argument
EcoSoftware
Training C/C++
20
Converting Strings to Numerical Values

Examples:
EcoSoftware
Training C/C++
21
Working with Wide Character Strings




Using the <wchar.h> header file
Store a wide character string in an array of elements of type
wchar_t.
Wide character string constant just needs the L modifier in
front of it.
Exampe :


wchar_t proverb[] = L"A nod is as good as a wink to a blind horse.";
Display value.

printf("The proverb is:\n%S", proverb);
EcoSoftware
Training C/C++
22
Operations on Wide Character Strings


Using the <wchar.h> header file declares a range of functions for operating on
wide character strings.
Functions That Operate on Wide Character Strings in <wchar.h>.
Function
Description
wcslen(const wchar_t* ws)
Returns a value of type size_t that is the length
of the wide character string ws that you pass
as the argument. The length excludes the
termination L'\0' character.
wcscpy(wchar_t* destination,
const wchar_t source)
Copies the wide character string source to the wide
character string destination. The function returns
source.
wcscat(whar_t* ws1,
whar_t* ws2)
Appends a copy of ws2 to ws1. The first character of
Ws2 overwrites the terminating null at the end of
ws1. The function returns ws1.
EcoSoftware
Training C/C++
23
Operations on Wide Character Strings
Function
Description
wcsncpy(wchar_t* destination,
const wchar_t source,
size_t n)
Copies n characters from the wide character string
source to the wide character string destination.
wcsncmp(const wchar_t* ws1,
const wchar_t* ws2)
Compares the wide character string pointed to by ws1
with the wide character string pointed to by ws2 and
returns a value of type int
wcscmp(const wchar_t* ws1,
const wchar_t* ws2, size_t n)
Compares up to n characters from the wide character
string pointed to by ws1 with the wide character string
pointed to by ws2.
wcsstr(const wchar_t* ws1,
const wchar_t* ws2)
Returns a pointer to the first occurrence of the wide
character string ws2 in the wide character string ws1.
EcoSoftware
Training C/C++
24
Testing and Converting Wide Characters

The <wchar.h> header also declares functions to test for specific
subsets of wide characters
Function name
iswlower()
iswupper()
iswalpha()
iswalnum()
iswcntrl()
iswprint()
iswgraph()
iswdigit()
iswxdigit()
iswblank()
iswspace()
iswpunct()

EcoSoftware
Test for
Lowercase letter
Uppercase letter
Uppercase or lowercase letter
Uppercase or lowercase letter or a digit
Control character
Any printing character including space
Any printing character except space
Decimal digit (L'0' to L'9')
Hexadecimal digit (L'0' to L'9', L'A' to L'F', L'a' to L'f')
Standard blank characters (space, L'\t')
Whitespace character (space, L'\n', L'\t', L'\v', L'\r', L'\f')
Printing character for which isspace() and isalnum() return
false

Training C/C++
25
Example Converting Wide Characters
EcoSoftware
Training C/C++
26
End
• Thank You
EcoSoftware
Training C/C++
27

similar documents