CSE-141: Structured Programming CSE-141: Structured Programming
CSE-141: Structured Programming CSE-141: Structured Programming
4
String Variable Declaration
Array of characters:
5
String Variable
Typically a partially filled array
Declare large enough to hold max-size string, including
the null character.
Given a standard array:
6
String Variable Initialization
Can initialize string:
7
String Variable Initialization
Can omit array-size:
8
String Indexes
A string IS an array
Can access indexed variables of:
hi[0] is ‘H’
hi[1] is ‘i’
hi[2] is ‘\0’
hi[3] is unknown
hi[4] is unknown
9
String Index Manipulation
Can manipulate array elements
Be careful!
Here, ‘\0’ (null) was overwritten by a ‘!’
If null overwritten, string no longer ‘acts’ like a string!
Unpredictable results!
10
String Library
Used for string manipulations
Normally want to do ‘fun’ things with strings
Requires library string.h:
http://en.wikipedia.org/wiki/String.h
11
= with strings
Strings are not like other variables, they are arrays
Cannot assign:
12
== with strings
Cannot use operator == to compare
13
String Length: strlen
Often useful to know length of string
strlen(string)
Returns number of characters
Does not include null
Return type is size_t so type cast may be required
14
String Concatenate: strcat
Appends one string onto end of another
strcat(destination, source)
15
String Parameters to Functions
A string is an array, so
String parameter is an array parameter
Strings passed to a function can be changed by the
receiving function!
Like all arrays, typical to send size as well
Function could also use ‘\0’ to find end
16
String Input and Output
Watch input size of string
Must be large enough to hold entered string!
+ ‘\n’ perhaps
+ ‘\0’
C gives no warnings of input size issues!
Functions in stdio.h
17
Character Input: getchar
Reads one character at a time from a text stream
int getchar( )
Reads the next character from the standard input stream
and returns its value
Return type is int!
Will convert if assigned to char
18
Character Output: %s and putchar
Format string placeholder for string: %s
putchar: Writes one character at a time
int putchar (int outChar)
Writes the parameter to standard output
If successful, returns the character written
19
String variable
20
String variable
Use stdin for now
String Input: fgets
char *fgets (char * strPtr, int size, FILE *fp)
Inputs characters from the specified file pointer
through \n or until specifed size is reached
Puts newline (\n) in the string if size not
reached!!!
Appends \0 at the end of the string
If successful, returns the string & places in
argument
21
String variable or constant
22
String variable
Use stdout for now
String Output: fputs
int fputs (const char *strPtr, FILE *fp)
Takes a null-terminated string from memory and writes
it to the specified file pointer
Drops \0
Programmer's responsibility: Make sure the newline is
present at the appropriate place(s)
23
References
• https://
www.programiz.com/c-programming/c-strings
• https://www.codesdope.com/c-string/
Allah Hafez