Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 103 Engineering Programming Chapter 25 C Strings, Part 1

Similar presentations


Presentation on theme: "ECE 103 Engineering Programming Chapter 25 C Strings, Part 1"— Presentation transcript:

1 ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
Herbert G. Mayer, PSU CS Status 7/24/2014 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE

2 Syllabus String Data Simple String Operations Examples

3 String Data C lacks the built-in type string
Strings are approximated in C via arrays of characters Declaration: char stringname[ SIZE ]; Characters in the string occupy consecutive elements in the array 2

4 The first '\0' (null) character found in the array marks the end of the string. It must be present
When declaring a string, the array size must be big 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

5 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

6 A literal string automatically has a '\0' at the end of the string
A literal string 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 literal string constant Example: char s[10] = "Hi, Bob!"; char mystr[] = " Good night everybody! ”; char t1[4] = "Jim"; // OK char t2[3] = "Jim"; // ERROR! Overflow 5

7 Example: char s[10] = "Hi, Bob!";
',' ' ' 'B' 'o' 'b' '!' '\0' 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 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 = 10 Valid array index range = 0 to 9 Space needed to store string = 9 Valid string index range = 0 to 8 String length = 8 6

8 What happens if we do this: s[6] = '\0'; ?
Example: char s[10] = "Hi, Bob!"; What happens if we do this: s[6] = '\0'; ? The string stored in s is now “Hi, Bo” instead of “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' 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' '!' 7

9 To embed a quotation mark in a string, use \" Example:
char str1[]="Hello"; /* Hello */ char str2[]="\"Hello\""; /* "Hello" */ To embed a backslash in a string, use \\ char winpath[]="C:\\windows"; /* C:\windows */ 8

10 Examples of Simple String Operations
Assume these string declarations: char str1[ MAXSIZE ], str2[ MAXSIZE ]; Input a string (requires stdio.h) fgets( str1, MAXSIZE, stdin ); Print a string printf( "str1 = %s\n", str1 ); Copy a string (requires string.h) strcpy( str1, "Hello” ); strcpy( str2, str1 ); Stores input in str1. Copies “Hello” to str1. Copies contents of str1 to str2. 9

11 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


Download ppt "ECE 103 Engineering Programming Chapter 25 C Strings, Part 1"

Similar presentations


Ads by Google