EEL 3801 Part IV Fundamentals of C and C++ Programming - Input/Output and String Manipulations.

Download Report

Transcript EEL 3801 Part IV Fundamentals of C and C++ Programming - Input/Output and String Manipulations.

EEL 3801
Part IV
Fundamentals of C and C++
Programming
- Input/Output and String
Manipulations
Strings
Strings
• Strings in C are arrays of characters that end
in the null character ‘\0’
• A string is accessed through a pointer to the
first element, just like a regular array.
• Declared as:
char color[] = “blue”;
char *colorptr = “blue”;
char color[] = {‘b’,‘l’,‘u’,’e’,’\0’};
Strings in Memory
• Array must be large enough to write longest
string + the null character.
• If a string longer than the array is to be
written, C will over write the adjoining
memory element at the end of the string,
thus erasing any value that might be there.
Character Handling Library
• Contains several functions that perform
useful tests on characters.
• Included in the ctype.h header file.
• Require a character or EOF as an argument.
• Typically used to determine some
information about the passed character.
• See page 321 of text for list of functions.
String Conversion Functions
• Convert strings to floating point or to
integer values.
• Found in stdlib.h.
• Most common are:
– atof(): converts string to floating point.
– atoi(): converts string to integer.
– atol(): converts string to long integer
• See pages 326 to 329 of textbook.
String Manipulation Functions
• Manipulating string data is different from
manipulating other data.
• This library provides the necessary
functions.
• Found in the string.h header file.
String Manipulation Functions
• strcpy(s1,s2): copies s2 into s1 and
returns value of s1.
• strncpy(s1,s2,n): copies at most the
first n characters of s2 into s1 and returns
value of s1.
• strcat(s1,s2): appends string s2 to
s1. The first character of s2 overwrites null
character of s1. Returns value of s1.
String Manipulation Functions
• strncat(s1,s2,n): appends at most
the first n characters of s2 to s1. S1’s null
character overwritten by first character of
s2. Returns value of s1.
• strcmp(s1,s2): compares s1 and s2.
Returns 0 if s1 = s2; < 0 if s1 < s2; > 0 if s1
> s2.
• strncmp(s1,s2,n):comps first n of s1
String Manipulation Functions
• Several other functions exist to:
– search a string for a particular character, with
several variations.
– manipulate memory blocks
– count the length of a string.
• See Chapter 8 of textbook.
Input/Output
Streams
• All input and output performed through
streams.
• Sequences of characters organized into lines
which end with the newline character.
• C implementation must be able to support at
least 254 characters, including newline
Streams
• Three standard streams:
– standard input stream - keyboard
– standard output stream - the video screen
– standard error stream - the video screen
• Can be redirected
Inputs Functions in C
• scanf() is the basic input function in C.
• Requires that the data type be identified
within quotes.
• The variable name of the datum read is
indicated by its address-of operator.
scanf(“%d%d%d”, &a, &b, &c);
• See pages 379-384 of textbook.
scanf() and Strings
• scanf() will read characters until space,
newline or eof indicator is encountered.
• For a character array to be printed as a
string, the last character must be the null
character.
String Inputs Functions in C
• Some special functions exist to read and
write strings.
• gets() reads characters from the standard
input stream and stores it in its argument
array.
• Stops when sees a newline or eof.
• A null character is appended automatically
to the array.
String Inputs Functions in C
• getchar() inputs the next character from
the standard input and returns it as an
integer.
• Does not take an argument.
• Must be put in a loop if reading string.
• Null character not automatically appended.
Output Functions in C
• The printf() function is the basic output
function in C.
String Output Functions in C
• Some output functions exist specifically for
strings and characters.
• puts() prints a string to the standard
output.
• Followed by a newline.
• Returns an integer value
String Output Functions in C
• putchar() prints its character argument.
• Must be placed within a loop if writing a
string.