Transcript Strings

CS 162
Introduction to Computer Science
Chapter 17
C++ String Objects
Herbert G. Mayer, PSU
(Copied from Prof. Phillip Wong at PSU)
Status 11/30/2014
Syllabus







String Data
Simple String Operations
Examples
String I/O Functions
String Library Functions
String to Number Conversion
Arrays of Strings
String Data




C++ lacks the built-in type string
Strings are approximated in C++ via arrays of
characters, i.e. arrays of type char
Declaration: char stringname[ SIZE ];
Characters in the string occupy consecutive char
elements in the array
2

The first '\0' (null) character found in a string literal
marks the end of the string. It must be present for
literals, inserted by your C++ compiler

When declaring a string, the array size should also be
long enough to hold both the string and '\0’

Declaring a character array larger than the expected
string length allows the array to accommodate a
larger string at a later time
3
Example:
// Declare two strings
char str1[ 4 ];
char str2[ 10 ];
// Initialize string one char at a time
str1[0]= 'C'; str1[1]= 'a'; str1[2]= 't';
str1[3]= '\0';
// Use up only 5 out of 10 elements
str2[0]= 'J'; str2[1]= 'a'; str2[2]= 'n';
str2[3]= 'e'; str2[4]= '\0';
4



A string literal, A.K.A. constant is delimited by
double quotation marks (e.g., "Hello, world!”)
A literal string automatically has a '\0' at the end of
the string
A string variable can be declared and initialized with
a string literal
Example:
char
char
char
char
s[10] =
mystr[]
t1[4] =
t2[3] =
"Hi, Bob!";
= " Good night everybody! ”;
"Jim"; // OK
"Jim"; // ERROR! Overflow
5
Example:
char s[10] = "Hi, Bob!";
s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9]
s → 'H' 'i' ',' ' ' 'B' 'o' 'b' '!'
s[0] 'H'
1218
s[1] 'i'
1219
s[2] ','
1220
s[3] ' '
1221
s[4] 'B'
1222
s[5] 'o'
1223
s[6] 'b'
1224
s[7] '!'
1225
s[8] '\0'
1226
s[9]
1227
'\0'
s is the address of the first character in the string.
s[j] is the j-th character in the string.
s is equivalent to &s[0].
Declared array size
Valid array index range
= 10
= 0 .. 9
6
Example:
char s[10] = "Hi, Bob!";
s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9]
s → 'H' 'i' ',' ' ' 'B' 'o' 'b' '!'
'\0'
What happens if we do this: s[6] = '\0'; ?
s[0] s[1] s[2] s[3] s[4] s[5] s[6] s[7] s[8] s[9]
s → 'H' 'i' ',' ' ' 'B' 'o'
'\0' '!' '\0'
The string stored in s is now “Hi, Bo” instead of
“Hi, Bob!”
7

To embed a quote " in a string, use \"
Example:
char str1[] = "Hello";
char str2[] = "\"Hello\"";

/* Hello */
/* "Hello" */
To embed a backslash in a string, use \\
Example:
char winpath[]="C:\\Windows”; // C:\Windows
8
Examples of Simple String Operations
Assume these string declarations:
char str1[ MAXSIZE ], str2[ MAXSIZE ];

Input a string (requires stdio.h)
fgets( str1, MAXSIZE, stdin );

Stores input in str1.
Print a string
printf( "str1 = %s\n", str1 );

Copy a string (requires string.h)
strcpy( str1, "Hello” ); Copies literal “Hello” to str1.
strcpy( str2, str1 );
Copies contents of str1 to str2.
9
Example:
/* Simple string operations demonstration */
#include <stdio.h>
#include <string.h>
#define MAXLEN 100
int main( void )
{ // main
char str1[ MAXLEN ], str2[ MAXLEN ];
printf( "What is your name? ” );
fgets( str1, MAXLEN, stdin );
printf( "Hello, %s!\n", str1 );
return 0;
} //end main
Output:
What is your name? Joe
Hello, Joe
!
Why does the exclamation point
show up on a separate line?
fgets() stores the newline
('\n') that you typed as part of
the string itself.
10
String I/O Functions



C has library functions to input and output strings
Use #include <stdio.h>
In the following table, char * is a pointer to a
character array
String I/O Functions
int printf( const char * fmt, … )
Prints formatted output to console
int sprintf( char * s, const char * fmt, … )
Prints formatted output to string s
int puts( const char * s )
Prints string to console
int scanf( const char * fmt, … )
Reads formatted input from console
int fgets( const char * s, FILE * stream )
Reads string from input stream
11
printf()
int printf( const char * format, …);
Writes formatted output to the console:
Use the %s format specifier for strings
 %s expects the address of a char array which contains a string

Example:
char name[] = "Jane Doe";
printf("[%s]\n", name );
→ [Jane Doe]
printf("[%s]\n", & name[0] ); → [Jane Doe]
printf("[%s]\n", & name[5] ); → [Doe]
12
sprintf()
int sprintf( char * s, const char * format, …);
Performs a formatted print and saves it into a string
Works like printf except the output is stored in string s
 Nothing is printed to the console

Example:
char str[50];
int x = 4;
sprintf( str, "x=%d y=%f", x, 1.5f );
printf( "title = %s\n", str );
title = x=4 y=1.500000
<- and line feed
13
puts()
int puts( const char * s );
Writes a string to the console

A newline '\n' is automatically printed at the end of the string
Example:
char name[] = "Jane Doe";
puts( name );
printf( "lives here.\n” );
Output:
Jane Doe
lives here.
14
scanf()
int scanf( const char * format, …);
Reads formatted input from the console
Use the %s format specifier for strings
 %s expects the address of a char array
 Only reads single “words” at a time (whitespace delimited)

Example:
char str[100];
/* String storage */
scanf( "%s", str ); /* str is address */
scanf( "%s", &str[0] ); // synonymous to str
15
scanf()

scanf skips whitespace( space, tab, newline) and then stores
in str all characters up to the next whitespace

scanf automatically adds '\0' to the end of the array

It does not check for char array overflow. The array must be
large enough to hold the expected string and the '\0’

Any unused text remains in the input stream
Example:
scanf( "%s", str1 );
User types in: Hello, Portland !!!
scanf( "%s", str2 );
→ str1 holds Hello,
→ str2 holds Portland
// not the !!!
16
fgets()
char * fgets( char * s, int m, FILE * stream );
Reads a string from the input stream
For input from the console, use stdin for the stream
fgets reads input characters until one of these conditions is met:
m – 1 characters are read
 '\n' (newline) is read, will be stored if fits into space
 End-of-file is detected
If successful, returns pointer to s
If end-of-file, returns NULL pointer

17
Example
#define MAXLEN 10000
char str[ MAXLEN ];
// String storage
if( fgets( str, MAXLEN, stdin ) ) {
printf( "%s", str );
}else{
printf( "End of file\n” );
} //end if
18

If a newline is entered to signal the end of input, the '\n'
character is also stored as part of string

fgets automatically terminates the string with '\0’
Example:
fgets( str, 10, stdin );
Suppose the user types in: Jim May
str
→
User types
Enter key
(newline)
'J' 'i' 'm' ' ' 'M' 'a' 'y' '\n' '\0'
printf( "[%s]", str );
]
→
[Jim May
19
Is there a simple way to get rid of the extraneous '\n' when
using fgets?
This is one approach that works:
fgets( s, MAXLEN, stdin );
if( strchr( s,'\n') != NULL )
s[ strlen(s) - 1 ] = '\0';
Caveat: The if check is necessary because a Ctrl-D terminates
input without adding an extra '\n’
Warning: There is also a function; gets(). DO NOT USE IT!!
gets() does not check for char array overflow
20
String Library Functions

C has a library of string processing functions
Use #include <string.h>

In the following table:

s is of type char * (pointer to char array)
n is of type size_t (unsigned integer)
cs and ct are of type const char *
c is an int converted to char
21
Some Common string lib functions
size_t strlen( cs )
return length of cs.
char * strcpy( s, ct )
copy string ct to string s, including ‘\0’; return s.
char * strncpy( s, ct, n )
copy at most n characters of string ct to s; return s. Pad
with ‘\0’s if ct has fewer than n characters.
char * strcat( s, ct )
concatenate string ct to end of string s; return s.
char * strncat( s, ct, n )
concatenate at most n characters of string ct to string s,
terminate s with ‘\0’; return s.
int strcmp( cs, ct )
compare string cs to string ct; return <0 if cs<ct;
0 if cs==ct, or >0 if cs>ct.
int strncmp( cs, ct, n )
compare at most n characters of string cs to string ct;
return <0 if cs<ct; 0 if cs==ct, or >0 if cs>ct.
char * strchr( cs, c )
return pointer to first occurrence of c in cs or NULL if not
present
char * strrchr( cs, c )
return pointer to last occurrence of c in cs or NULL if not
present
char * strstr( cs, ct )
return pointer to first occurrence of string ct in cs, or NULL
if not present
22
strlen()
size_t strlen( const char * s );
Determines the length of the string s.
The function counts the number of characters in the array.
 The count starts at array index 0.
 It continues counting until the first '\0' is found.
('\0' itself is not included in the string length.)

Example:
strlen("") → 0
char x[] = "Cat"; strlen(x) → 3
SL = strlen(" PSU Vikings") → 12
23
strcpy()
char * strcpy( char * d, const char * s );
Copies contents of string s to string d
The terminating '\0' is also copied to d.
 The contents of d are overwritten.
 The address of string d is returned.
 Strings cannot be copied using the = assignment operator. Use
strcpy() instead.

Example:
char dst[20], src[] = "Hello";
strcpy( dst, "Siri” );
→ dst holds Siri
strcpy( dst, src );
→ dst holds Hello
24
strcat()
char * strcat( char * d, const char * s );
Concatenates contents of string s to string d
The contents of s are appended to the end of whatever is
already in d
 The original '\0' in d is deleted before appending happens
 The address of the new string d is returned
 Strings cannot be concatenated using the + operator in C.
Use strcat() instead.

Example:
char dst[20] = "PSU", src[] = " rocks!";
strcat( dst, src ); → dst holds PSU rocks!
25
strcmp()
int strcmp( const char * s, const char * t );
Compares contents of string s to string t
s and t are compared character by character
 Returns zero (0) if the strings are identical
positive number if s is lexically greater than t
negative number if t is lexically greater than s
 Strings cannot be compared using a relational operator. Use
strcmp() instead

Example:
char s1[5] = "PSU", s2[10] = "OSU";
strcmp( s1, s2 );
→ returns 1
strcmp( s2, s1 );
→ returns -1
26
Example:
char a[10], b[10];
/* Two strings */
strcpy(
strcpy(
strcat(
strcat(
/*
/*
/*
/*
a,
b,
a,
b,
"Star” );
a );
" Trek” );
" Wars” );
a:
b:
a:
b:
Star
Star
Star
Star
*/
*/
Trek */
Wars */
if( strlen(a) > 0 && strlen(b) > 0 )
if( strcmp( a, b ) == 0 )
printf( "You're kidding, right?\n” );
else if( strcmp( a, b ) < 0 )
printf( "Trekker!\n” );
27
strchr()
char * strchr( const char * s, int c );
Finds position of character c within string s.

If c is found, returns pointer to first occurrence of c in s.
If c is not found, returns NULL pointer.
Example:
char s[] = "PSU OSU";
strchr( s, 'A’ );
→ returns NULL
p = strchr( s, 'U’ ); → returns pointer to first U
28
strchr()
char * strstr( const char * s, const char * t
);
Finds position of string t within string s.

If t is found, returns pointer to first occurrence of t in s.
If t is not found, returns NULL pointer.
Example:
char s[] = "PSU OSU";
strstr( s, "A" );
→ returns NULL
p = strstr( s, "U O" ); → returns ptr to location
29
String to Number Conversion

Use #include <stdlib.h>
Conversion functions (partial list)
double atof( const char * s )
Converts s to a number of type double
int atoi( const char * s )
Converts s to a number of type int
long atol( const char * s )
Converts s to a number of type long int
Example:
double x;
char numstr[] = "12.75 HI";
x = atof( numstr ); → x contains 12.75
x = atof( "HI 12.75” ); → x contains 0.0
printf( "%d\n", atoi( numstr ) ); → displays 12
30
Array of Strings


An array of strings (“string array”) is a 2-D array
Each row represents a separate string

When declaring the array, the number of columns
must be large enough to hold the largest expected
string

An individual string within the array can be accessed
by using just the row index
31
Example:
/* 4 strings of up to 10 chars each */
char s[4][10];
strcpy( s[0], "Doe, Jane” );
strcpy( s[3], "Rand, Bob” );
/* Assume user enters: Li, Joe */
fgets( s[1], 10, stdin );
/* Assume user enters:
scanf( "%s", s[2] );
Li, Joe */
printf( "%s\n", s[0] );
printf( "%s\n", &s[0][5] );
printf( "%c\n", s[3][6] );
/* Doe, Jane */
/* Jane */
/* B */
32
Example: (continued from previous page)
0
1
2
3
4
5
6
7
8
9
s[0]
→
'D' 'o' 'e' ',' ' ' 'J' 'a' 'n' 'e' '\0'
length = 9
s[1]
→
'L' 'i' ',' ' ' 'J' 'o' 'e' '\n' '\0'
length = 8
s[2]
→
'L' 'i' ',' '\0'
length = 3
s[3]
→
'R' 'a' 'n' 'd' ',' ' ' 'B'
o'
'b' '\0'
length = 9
s is the name for the entire array of strings.
s[i] is the address of the i-th string in the array.
s[i][j] is j-th character of the i-th string.
s[i] is equivalent to &s[i][0].
33
Example: Write a string length function using arrays
#include <stdio.h>
/* Function returns the length of a string */
int str_len( const char s[])
{ // str_len
int k = 0; /* Array index for string */
while( s[k] != '\0') /* Look for string terminator */
k++;
return k; /* k is also the length */
} //end str_len
int main( void )
{ // main
char a[] = "Hello!";
printf( "String length is %d.\n", str_len(a) );
return 0;
} //end main
34
Example: Write a string copy function using arrays
/* String copy: source: cs thus can be const, destination: st */
char * str_cpy( char st[], const char cs[] )
{
int k = 0; // Array index for string
while( cs[k] != '\0’ ) { /* Look for string terminator */
st[k] = cs[k]; /* Copy single character at a time */
k++;
} //end while
st[k] = '\0'; /* Add string terminator to destination */
return st; // st by itself is an address
} //end str_cpy
35