5 - CStrings
Transcript of 5 - CStrings
-
8/8/2019 5 - CStrings
1/24
1/19/10
Programming in C #3
Characters and StringsCharacters and Strings
-
8/8/2019 5 - CStrings
2/24
1/19/10
ASCII
TheAmerican Standard Code for Information InterchangeTheAmerican Standard Code for Information Interchange (ASCII)(ASCII) character set,character set,has 128 characters designed to encode the Roman alphabet used in English andhas 128 characters designed to encode the Roman alphabet used in English andother Western European languages.other Western European languages.
C was designed to work withASCII and we will only use theASCII character set inC was designed to work with ASCII and we will only use theASCII character set in
this course. Thethis course. The char data type is used to storeA
SCII characters in Cdata type is used to storeA
SCII characters in C ASCII can represent 128 characters and is encoded in one eight bit byte with aASCII can represent 128 characters and is encoded in one eight bit byte with a
leading 0. Seven bits can encode numbers 0 to 127. Since integers in the range ofleading 0. Seven bits can encode numbers 0 to 127. Since integers in the range of0 to 127 can be stored in 1 byte of space, the0 to 127 can be stored in 1 byte of space, the sizeof(char)sizeof(char) is 1.is 1.
The characters 0 through 31 representThe characters 0 through 31 representcontrol characterscontrol characters (e.g., line feed, back(e.g., line feed, backspace), 32space), 32--126 are126 areprintable charactersprintable characters, and 127 is, and 127 is deletedelete ..
-
8/8/2019 5 - CStrings
3/24
1/19/10
char type
C supports theC supports the char data type for storing a single character.data type for storing a single character.
char uses one byte of memoryuses one byte of memory
char constants are enclosed in single quotesconstants are enclosed in single quotes
char myGrade = A;char yourGrade = ?;
-
8/8/2019 5 - CStrings
4/24
1/19/10
ASCII Character Chart
-
8/8/2019 5 - CStrings
5/24
1/19/10
Special Characters
The backslash character,The backslash character, \\, is used to indicate that the char that, is used to indicate that the char thatfollows has special meaning. E.g. for unprintable characters andfollows has special meaning. E.g. for unprintable characters andspecial characters.special characters.
For exampleFor example \n is the newline character
\t is the tab character
\ is the double quote (necessary since double quotes are used to enclosestrings
\is the single quote (necessary since single quotes are used to enclose chars
\\ is the backslash (necessary since \ now has special meaning
\a is beep which is unprintable
-
8/8/2019 5 - CStrings
6/24
1/19/10
Special Char Example Code
What is the output from these statements?What is the output from these statements?
printf(\t\tMove over\n\nWorld, here I come\n");
Move over
World, here I come
printf("I\ve written \Hello World\\n\t many times\n\a);
Ive written Hello World
many times
-
8/8/2019 5 - CStrings
7/24
1/19/10
Character Library
There are many functions to handle characters. To use theseThere are many functions to handle characters. To use these
functions in your code,functions in your code,
#include
Note that the function parameter type isNote that the function parameter type is int, not, notchar. Why is this. Why is thisok?ok?
Note that the return type for some functions isNote that the return type for some functions is int since ANSI Csince ANSI Cdoes not support thedoes not support thebool data type. Recall that zero is false,data type. Recall that zero is false,nonnon--zero is true.zero is true.
A few of the commonly used functions are listed on the next slide.A few of the commonly used functions are listed on the next slide.For a full list ofFor a full list ofctype.h functions, typefunctions, typeman ctype.h at theat the
unix prompt.unix prompt.
-
8/8/2019 5 - CStrings
8/24
1/19/10
ctype.h
int isdigit (int c);
Determine if c is a decimal digit (0- 9)
int isxdigit(int c);
Determines if c is a hexadecimal digit (0- 9, a- f, or A- F)
int isalpha (int c););
Determines if c is an alphabetic character (a- zor A- Z)
int isspace (int c);
Determines if c is a whitespace character (space, tab, etc)
int isprint (int c);
Determines if c is a printable character
int int tolower (int c);
int toupper (int c);
Returns c changed to lower- or upper-case respectively, if possible
-
8/8/2019 5 - CStrings
9/24
1/19/10
Array of char
An array of chars may be (partially) initialized. This declarationAn array of chars may be (partially) initialized. This declaration
reserves 20 char (bytes) of memory, but only the first 5 are initializedreserves 20 char (bytes) of memory, but only the first 5 are initializedchar name2 [ 20 ] = { B, o, b, b, y };
You can let the compiler count the chars for you. This declarationYou can let the compiler count the chars for you. This declarationallocates and initializes exactly 5 chars (bytes) of memoryallocates and initializes exactly 5 chars (bytes) of memorychar name3 [ ] = { B, o, b, b, y };
An array of chars is NOT a stringAn array of chars is NOT a string
-
8/8/2019 5 - CStrings
10/24
1/19/10
Character Input/Output
UseUse %c ininprintf( )andandfprintf( )to output a singleto output a singlecharacter.character.
char yourGrade = A;
printf( Your grade is %c\n, yourGrade);
Input char(s) usingInput char(s) using %c withwith scanf( ) ororfscanf( )
char grade, scores[3];
%c inputs the next character, which may be whitespace
scanf(%c, &grade); %nc inputs the next n characters, which may include whitespace. NO \0is
appended to the input chars.
scanf( %3c, scores); // note -- no & needed
-
8/8/2019 5 - CStrings
11/24
1/19/10
Strings in C
In C, a string is an array of characters terminated with the null character (In C, a string is an array of characters terminated with the null character (\\0, value = 0,0, value = 0,seeASCII chart).seeASCII chart).
A string may be defined as a char array by initializing the last char to A string may be defined as a char array by initializing the last char to \\00
char name4[ 20 ] = {B, o, b, b, y, \0 };
Char arrays are permitted a special initialization using a string constant. Note that theChar arrays are permitted a special initialization using a string constant. Note that the
size of the array must account for the size of the array must account for the \\0character0character
char name5[6] = Bobby;
Or let the compiler count the chars and allocate the appropriate array sizeOr let the compiler count the chars and allocate the appropriate array size
char name6[ ] = Bobby;
All string constants are enclosed in double quotes and include the terminating All string constants are enclosed in double quotes and include the terminating \\00
charactercharacter
-
8/8/2019 5 - CStrings
12/24
1/19/10
C String Library
C provides a library of string functions.C provides a library of string functions.
To use the string functions, includeTo use the string functions, include..
Some of the more common functions are listed here on the nextSome of the more common functions are listed here on the next
slides.slides. To see all the string functions, typeTo see all the string functions, typeman string.hman string.h at the unix prompt.at the unix prompt.
-
8/8/2019 5 - CStrings
13/24
1/19/10
C String Library (2)
Commonly used string functionsCommonly used string functions
These functions look for the These functions look for the \\0character to determine the end / size0character to determine the end / sizeof the stringof the string strlen( char string[ ] )
Returns the number of characters in the string, not including the null character
strcpy( char s1[ ], char s2[ ] )
Copies s2 on top of s1. The order of the parameters mimics the assignmentoperator
strcmp ( char s1[ ] , char s2[ ] )
Returns < 0, 0, > 0 if s1 < s2, s1 == s2 or s1 > s2 lexigraphically strcat( char s1[ ] , char s2[ ])
Appends (concatenates) s2 to s1
-
8/8/2019 5 - CStrings
14/24
1/19/10
C String Library (3)
Some function in the C String library have an additional sizeSome function in the C String library have an additional size
parameter.parameter.
strncpy( char s1[ ], char s2[ ], int n )
Copies at most n characters of s2 on top of s1. The order of the parameters
mimics the assignment operator
strncmp ( char s1[ ] , char s2[ ], int n )
Compares up to n characters of s1 with s2
Returns < 0, 0, > 0 if s1 < s2, s1 == s2 or s1 > s2 lexigraphically
strncat( char s1[ ], char s2[ ] , int n)
Appends at most n characters of s2 to s1
-
8/8/2019 5 - CStrings
15/24
1/19/10
String Code
char first[10] = bobby;
char last[15] = smith;
char name[30];
char you[ ] = bobo;
strcpy( name, first );strcat( name, last );
printf( %d, %s\n, strlen(name) name );
strncpy( name, last, 2 );
printf( %d, %s\n, strlen(name) name );
int result = strcmp( you, first );
result = strncmp( you, first, 3 );
strcat( first, last );
-
8/8/2019 5 - CStrings
16/24
Simple Encryptionchar c, msg[] = "this is a secret message";
int i = 0;char code[26] = /* Initialize our encryption code */{'t','f','h','x','q','j','e','m','u','p','i','d','c','k','v','b','a','o','l','r','z','w','g','n','s','y'} ;
/* Print the original phrase */
printf ("Original phrase: %s\n", msg);
/* Encrypt */while( msg[i] != '\0 ){
if( isalpha( msg[ i ] ) ) {c = tolower( msg[ i ] ) ;
msg[ i ] = code[ c - a ] ;}++i;
}printf("Encrypted: %s\n", msg ) ;
-
8/8/2019 5 - CStrings
17/24
1/19/10
String Output
UseUse %s ininprintf( ) ororfprintf( ) to print a string. All chars will be outputto print a string. All chars will be output
until the until the \\0character is seen.0character is seen.
char name[ ] = Bobby Smith;
printf( My name is %s\n, name);
As with all conversion specifications, a minimum field width andAs with all conversion specifications, a minimum field width and
justification may be specifiedjustification may be specified
printf (My name is %20s\n, name);
printf (My name is %-20s\n);
-
8/8/2019 5 - CStrings
18/24
1/19/10
String Input
UsingUsing %s withwith scanf( ) ororfscanf( ) will interpret the next set ofwill interpret the next set ofconsecutive nonconsecutive non--whitespace characters as a string, store it in thewhitespace characters as a string, store it in thespecified char array, and append a terminatingspecified char array, and append a terminating \0character.character.char name[22];
printf( Enter your name: );
scanf( %s, name); UsingUsing %ns withwith scanf( ) ororfscanf( ) will interpret the next set ofwill interpret the next set of
consecutive nonconsecutive non--whitespace characters up to a maximum ofwhitespace characters up to a maximum ofncharacters as a string, store it in the specified char array, and appendcharacters as a string, store it in the specified char array, and appenda terminatinga terminating \0character.character.
printf( Enter your name: );scanf(%21s, name);
-
8/8/2019 5 - CStrings
19/24
1/19/10
gets( ) to read a line
TheThe gets( ) function is used to read afunction is used to read a line of inputline of input (including the(including thewhitespace) fromwhitespace) from stdin until theuntil the \n character is encountered.character is encountered.TheThe \\n character is replaced with the terminatingn character is replaced with the terminating \\0 character.0 character.#include
char myString[ 101 ];gets( myString );
TheThe fgets( ) function is used to read afunction is used to read a line of inputline of input (including(includingthe whitespace) from the specified FILE until thethe whitespace) from the specified FILE until the \n character ischaracter isencountered or until the specified number of chars is read.encountered or until the specified number of chars is read.
-
8/8/2019 5 - CStrings
20/24
1/19/10
fgets( )
#include #include /* exit */
int main ( )
{
double x ;
FILE *ifp ;char myLine[42 ]; /* for terminating \0 */
ifp = fopen("test_data.dat", "r");if (ifp == NULL) {
printf ("Error opening test_data.dat\n");
exit (-1);
}
fgets(myLine, 42, ifp ); /* read up to 41 chars*/fclose(ifp); /* close the file when finished */
/* check to see what you read */
printf(myLine = %s\n, myLine);return 0;
}
-
8/8/2019 5 - CStrings
21/24
1/19/10
Detecting EOF with fgets( )
fgets( ) returns the memory address in which the line was storedfgets( ) returns the memory address in which the line was stored(the char array provided). However, when fgets( ) encounters(the char array provided). However, when fgets( ) encountersEOF, the special value NULL is returned.EOF, the special value NULL is returned.
FILE *inFile;FILE *inFile;
inFile = fopen( myfile, r );inFile = fopen( myfile, r );
/* check that the file was opened *//* check that the file was opened */
char string[120];char string[120];
while ( fgets(string, 120, inFile ) != NULL )while ( fgets(string, 120, inFile ) != NULL )
printf( %sprintf( %s\\n, string );n, string );
fclose( inFile );fclose( inFile );
-
8/8/2019 5 - CStrings
22/24
1/19/10
Big Enough
The owner of a string is responsible for allocating array spaceThe owner of a string is responsible for allocating array space
which is big enough to store the string (including the null character).which is big enough to store the string (including the null character).
scanf( ), fscanf( ), and gets( ) assume the char array argument is big enough
String functions that do not provide a parameter for the length rely onString functions that do not provide a parameter for the length rely onthe the \\0character to determine the end of the string.0character to determine the end of the string.
Most string library functionsMost string library functions do notdo not check the size of the stringcheck the size of the stringmemory. E.g.memory. E.g. strcpystrcpy
-
8/8/2019 5 - CStrings
23/24
23
What can happen?
int main( )int main( ){{char first[10] = "bobby";char first[10] = "bobby";char last[15] = "smith";char last[15] = "smith";
printf("first contains %d chars: %sprintf("first contains %d chars: %s\\n", strlen(first), first);n", strlen(first), first);printf("last contains %d chars: %sprintf("last contains %d chars: %s\\n", strlen(last), last);n", strlen(last), last);
strcpy(first, "1234567890123"); /* too big */strcpy(first, "1234567890123"); /* too big */
printf("first contains %d chars: %sprintf("first contains %d chars: %s\\n", strlen(first), first);n", strlen(first), first);printf("last contains %d chars: %sprintf("last contains %d chars: %s\\n", strlen(last), last);n", strlen(last), last);
return 0;return 0;
}}
/* output *//* output */first contains 5 chars: bobbyfirst contains 5 chars: bobbylast contains 5 chars: smithlast contains 5 chars: smithfirst contains 13 chars: 1234567890123first contains 13 chars: 1234567890123last contains 5 chars: smithlast contains 5 chars: smith
Segmentation faultSegmentation fault
-
8/8/2019 5 - CStrings
24/24
1/19/10
The Lesson
AvoidAvoidscanf( %s, buffer);scanf( %s, buffer);
UseUse scanf(%100s, buffer);scanf(%100s, buffer); insteadinstead
AvoidAvoidgets( );gets( ); UseUse fgets(stdin, .);fgets(stdin, .); insteadinstead