Strings advanced

Download Report

Transcript Strings advanced

ECE 103 Engineering Programming
Chapter 29
C Strings, Part 2
Herbert G. Mayer, PSU CS
Status 7/30/2014
Initial content copied verbatim from
ECE 103 material developed by
Professor Phillip Wong @ PSU ECE
Syllabus




String I/O Functions
String Library Functions
String to Number Conversion
Arrays of Strings
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 --strictly speaking, to a char-- that
contains the string
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
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
2
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]
3
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 );
4
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.
5
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
6
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
!!!
7
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
 End-of-file is detected
If successful, returns pointer to s
If end-of-file, returns NULL pointer

8
Example
#define MAXLEN 80
char str[ MAXLEN ];
/* String storage */
if( fgets( str, MAXLEN, stdin ) != NULL ) {
printf( "%s", str );
}else{
printf( "End of file\n” );
} //end if
9

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
10
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
11
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
12
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
13
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
14
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
15
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 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!
16
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
17
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” );
18
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
19
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
20
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
21
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
22
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 */
23
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].
24
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
25
Example: Write a string copy function using arrays
/* String copy: source is ct, destination is s */
char * str_cpy( char s[], const char ct[] )
{
int k = 0; /* Array index for string */
while( ct[k] != '\0’ ) { /* Look string terminator */
s[k] = ct[k]; /* Copy single character at a time */
k++;
} //end while
s[k] = '\0'; /* Add string terminator to destination */
return s; /* s by itself is an address */
} //end str_cpy
26