Sei sulla pagina 1di 36

Programming Lab (Lab5)

Strings, Pointers and Tools

Overview
A string is a sequence of characters. It is a kind of
data.
A literal string is a sequence of characters
enclosed by a pair of double quotes " ".
A string is stored as an array of char.
Strings are often processed using pointers.
The C standard library provides many useful string
handling functions.
2

Basic String Concept: Literal


String
#include <stdio.h>
int main(void)
{
printf("Hello, %s, morning!\n", "C Program");
return 0;
}

NULL
terminator is
added
automatically
at the end of
the literal
string.

Hello, C Program, morning!


Address

200

201

202

203

204

205

206

207

208

209

Position

Character

\0

Note

Characters in the literal string

NULL
3

Basic String Concept: Literal


String
A literal string is in fact a constant array of
constant characters.
The location (address) as well as the content of
the array is fixed, i.e. hello. This is similar to
constant like 123.
Modifying the content of a literal string in runtime will CRASH the program! (See the exercise
follows)

Creation of Literal String


When we write a C program, a pair of bare double quotes
represents a literal string, e.g., hello.
The compiler and the linker allocate memory for storing
the literal string automatically , excluding the double
quotes (optional).
Following the rule, there is an extra character reserved
automatically and put right after the literal string
content, the NULL (ASCII code 0) character.
The NULL terminator in fact signals the end of the string
since C does NOT keep the length of the string! (strlen())
5

Revision: char Array


A char array can be initialized with
a sequence of characters (in pairs of single quotes);
a sequence of ASCII codes.

The array size can be specified explicitly OR


implicitly.
The characters are stored consecutively.
The array content can be modified in run-time.
6

Basic String Concept: char


Array
NULL terminator is
#include <stdio.h>

added manually.
int main(void)
{
char name[15] = {'C',' ','P','r','o','g','r','a','m','\0'};
char greet[] = {109, 111, 114, 110, 105, 110, 103, 0};
printf("Hello, %s, %s!\n", name, greet);
NULL terminator is
return 0;
added manually.
}
Hello, C Program, morning!
Address

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

name[i]

10

11

12

13

14

Character

\0

ASCII code

67

80

114

111

103

114

97

109

Note

32

Characters in the string (char array) name

NULL

Unused space in the array


7

Char Array Usage Tips


The array size should be large enough for storing
the character content (trivial!). E. g., char
a[2]=hello;
We should reserve space and put the NULL
terminator at the end of the string. E.g.,
manually: char name[15] = {'C',
','P','r','o','g','r','a','m','\0'};

Drawback: there may be unused space left if the


declared array is too large.
8

Initializing a char Array with a


Literal
Initializing a char array
(string variable) with a
#include <stdio.h>
literal string (constant).
int main(void)
{
Note that there is
char name[15] = "C Program";
already a NULL
printf("Hello, %s, morning!\n", name);
terminator in the
return 0;
literal string.
}

Hello, C Program, morning!


Address

400

401

402

403

404

405

406

407

408

409

410

411

412

413

414

name[i]

10

11

12

13

14

Character

\0

Note

Characters in the string (char array) name

NULL

Unused space in the array


9

Initializing a char Array with a


Literal
It looks like "String Copy."
This is in fact initialization of an array
during variable declaration!
We CANNOT do assignment with an
array.
10

Exercise
#include <stdio.h>
int main(void)
{
char name[15] = "C Program";
name[1] = '-';
// Modify the content of the array name
name[6] = 0;
printf("Hello, %s, morning!\n", name);
return 0;
}
Hello, C-Prog, morning!

11

Exercise
#include <stdio.h>
int main(void)
{
char myName[15] = "C Program";
char yourName[30];
// Whole Array Assignment!
yourName = myName;
printf("Hello, %s, morning!\n", yourName);
return 0;

}
SYNTAX ERROR!!!

12

Exercise
#include <stdio.h>
int main(void)
{
char * ptr = "C Program";
ptr[1] = '-';
// Modify the content of the literal string which is constant!
ptr[6] = 0;
printf("Hello, %s, morning!\n", ptr);
return 0;
}
CRASH!!!

13

Exercise
#include <stdio.h>
int main(void)
{
char name[15] = "C Program";
char * ptr;
ptr = name;

// Equivalent: ptr = &name[0]


// i.e. ptr points to name[0]

ptr[1] = '-';
ptr[6] = 0;
printf("Hello, %s, morning!\n", name);
printf("Bye bye, %s!\n", ptr);
return 0;

Hello, C-Prog, morning!


Bye bye, C-Prog!
14

String Handling Functions


The standard library contains many
useful string handling functions.
They all require that strings passed
as arguments be NULL-terminated.
and well-allocated.
Their function prototypes are given in
the header file <string.h>.
15

Determining String Length


size_t

strlen( const char s[] )

Returns the number of characters (not including the


terminating NULL character '\0') in the string s
char str1[10] = "ABC";
char str2[] = "This is a string!";
int len;
len = strlen(str1);
printf("%d\n", len);

// coerce from size_t to int


// prints 3

printf("%d\n", strlen(str2));

// prints 17

printf("%d\n", strlen("Hello"));

// prints 5
16

Displaying Strings puts()


int

puts( char s[] )

Print the string s followed by a newline character.


What's the return value of puts()?
char s[] = "This is a string!";
puts(s);

// Same as printf("%s\n", s);

17

Getting Strings scanf()


#include <stdio.h>
int main(void)
{
char s1[20], s2[20];

this-is-first-string.
s1=this-is-first-string.
this is second string.
s2=this

&

scanf("%s", s1);
printf("s1=%s\n", s1);
printf("\n");
&

scanf("%s", s2);
printf("s2=%s\n", s2);
return 0;
}

When inputting a string


using scanf(), the
argument(s) does NOT
need to be preceded by
'&'. W H Y ?
White spaces (such as
space, tab) are regarded
as delimiters by scanf().
18

Getting Strings gets()


char *

gets( char s[] )

Input characters from the standard input into the array s


until a newline or the End-Of-File character is encountered.
A terminating null character is appended to the array and the
array s (i.e., the address of s[0]) is returned.

char name[20];
printf("Your name? ");
gets(name);
// we ignore the return value
printf("Hello %s!\n", name);
Your name? C Program
Hello C Program!
19

Copying strings
char * strcpy( char s1[], const char s2[] )
Copies s2 (including the NULL character) into s1
We should ensure s1 be large enough to store s2
(including the NULL character)
String s1 (i.e. the address of s1[0]) is returned
char s1[7] = "123456",
s2[7] = "ABC";

s1

'\0'

s2

'\0' ?

strcpy(s1, s2);

s1 becomes
C

'\0' 5

'\0'

// Output ABC in s1
printf("%s\n", s1);

Copy up to the NULL character. The


remaining characters in s1 are untouched.
20

Copying strings (with an


optional size)
char *

strncpy( char s1[], const char s2[],


size_t n )

If s2 has n or more characters, the


function copies the first n characters from
s2 into s1.
NO NULL character is appended to s1.
If s2 has less than n characters, the
function behaves like strcpy().
That is, it copies up to the NULL character.
21

strncpy() (Examples)
char s1[7] = "123456",
s2[7] = "ABC";
strncpy(s1, s2, 2);
// output AB3456
printf("%s\n", s1);
strncpy(s1, s2, 10);

s1

'\0'

s2

'\0' ?

'\0'

'\0' 5

'\0'

s1 becomes
A

s1 becomes
A

// output ABC
printf("%s\n", s1);
strncpy(s2, "12345", 4);
// Warning! s2 may NOT be
// NULL terminated!

s2 becomes
1

22

Concatenating strings
char * strcat( char s1[], const char s2[] )
Appends s2 to s1.

First character of s2 replaces the terminating


NULL character of s1.
We should ensure s1 is large enough to store
the concatenated string and a NULL character.
s1 is returned, s2 is untouched.
char s1[10] = "ABC";
char s2[20] = "123456";
strcat(s1, s2);
strcat(s2, "789");

// s1 becomes "ABC123456"
// s2 becomes "123456789"
23

Comparing strings
int strcmp( const char s1[], const char s2[] )
Compares s1 and s2 character-by-character (up to the
NULL character) based on their ASCII values
Returns
Zero if both strings are equal

A negative integer if s1 precedes (less than) s2


A positive integer if s1 succeeds (greater than)
s2
char s1[10], s2[20];
scanf("%s %s", s1, s2);
if ( strcmp(s1, s2) == 0 )
printf("Both strings are identical");
24

Comparing strings
s1

s2

"ABCD"

"ABC"

"ABC"
"AB C"
""
"123"

"abc"
"ABC"
"ABC"
"0178"

strcmp(s1,s2
)
0, -ve, or +ve?
s1

'\0'

s2

'\0' ?

s1

'\0' ?

s2

'\0' ?

s1

' ' C

s2

'\0' ?

s1

'\0' ?

s2

'\0' ?

s1

'\0' ?

s2

'\0'
?

'\0'
25

Other Useful Functions


#include <stdlib.h>

int atoi( const char str[] )


long atol( const char str[] )
The functions retrieve the longest leading
substring in str that represents a valid integer,
and returns the equivalent value as a value of type
int/ long.
Leading white-space characters are ignored.
int
a =
a =
a =
a =
a =

a;
atoi("1234");
atoi("
\n\n3123");
atoi("ABC10");
atoi("-1+1");
atoi("99.8e3");

//
//
//
//
//

a
a
a
a
a

becomes
becomes
becomes
becomes
becomes

1234
3123
0
-1
99
26

Other Useful Functions


#include <stdlib.h>

double atof( const char str[] )


The function retrieves the longest leading
substring in str that represents a valid floating
point number, and returns the corresponding
value as a double.
Leading white-space characters are ignored.
double x;
x = atof("3.14");
x = atof("
\n\n3.4");
x = atof("ABC10");
x = atof("-.344");
x = atof("1.2e3");
x = atof("1.4.4");

//
//
//
//
//
//

x
x
x
x
x
x

becomes
becomes
becomes
becomes
becomes
becomes

3.14
3.4
0.0
-0.344
1200.0
1.4
27

Remark
There are many other string handling
functions in the standard library.
Check the documentation to see if an existing
function suits your need before attempting to
write your own string handling functions.
Remember, browsing through reference
manuals is the essential technique that every
C programmer should equip with.
28

Strings and Pointers


#include <stdio.h>
#include <string.h>
int main(void)
{
char message[] = "Welcome to C!";
char *cptr;
cptr = message + 11;
printf("%s\n", cptr );
printf("%s\n", &message[11] );
return 0;
}
C!
C!
29

Memory Demonstration

message

67

\0

10 11 12 13 14
C

\0

cptr

cptr

30

Practice 1--Common Array


Two arrays are identical if they have the
same elements in the same order.
Algorithm for testing if two arrays, A and B,
are identical:
If both arrays have different number of elements,
then the array are different.
Otherwise, compare elements between A and B
one-by-one. If one of the elements is different,
then the arrays are different.
If none of the elements between A and B are
different, then the arrays are identical.
31

// Suppose A and B are two arrays of the same type


//
with n and m elements respectively.
int isIdentical = 1; // Assume A and B are identical
// this is a boolean FLAG (T/F)
if (n != m)
isIdentical = 0;
// set the flag to FALSE
else {
int i;
for (i = 0; i < n; i++)
if (A[i] != B[i]) {
isIdentical = 0; // set the flag to FALSE
break;
// stop the loop, save time!
}
}
if (isIdentical)
printf("A and B are identical.\n");
32

Practice 2 Substring Search


Locate a substring (s1) in a string (s2).
i.e., At which location does s1 first appear in s2?
s1

'\0'

s2 A

'\0' ?

'\0'

s2 A

'\0' ?

1st iteration
s1

s1 matches a substring starts at 1

2nd iteration
s1

'\0'

s2 A

'\0' ?

=
33

// Returns the location of substr" in "str".


// Returns -1 if "str" does not contain substr".
int locate( const char str[], const char substr[] ) {
int i, j, len1, len2;
len1 = strlen(str), len2 = strlen(substr);
// For all possible starting locations
for (i = 0; i <= (len1 len2); i++) {
int isIdentical = 1;
// For each substring started at location i
for (j = 0; j < len2; j++)
if ( substr[j] != str[i+j] ) {
Version 1
isIdentical = 0;
break;
}
if (isIdentical) // Return the 1st matching substr
return i;
}
return -1;
Substring Search
}

34

// Returns the location of substr" in "str".


// Returns -1 if "str" does not contain substr".
int locate( const char str[], const char substr[] ) {
int i, j, len1, len2;
len1 = strlen(str), len2 = strlen(substr);
// For all possible starting locations
for (i = 0; i <= (len1 len2); i++) {
if ( strncmp(substr, &str[i], len2) == 0 )
return i;
Substring Search
}
return -1;
using strncmp()
Version 2
}
// Returns the address of the first occurrence of the
// substring in the text. Returns NULL on not found.
char * p;
p = strstr("This is a text.", "is");
=> p "is is a text."
Version 3

Substring Search
using strstr()35

Challenging Yourselves Now


Today you face a white board but you
have the key parts.
No templates today !!!

36

Potrebbero piacerti anche