ECE 103 Engineering Programming Chapter 25 C Strings, Part 1

Slides:



Advertisements
Similar presentations
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
C-strings Array with base type char One character per indexed variable
CS 161 Introduction to Programming and Problem Solving Chapter 13 Console IO Herbert G. Mayer, PSU Status 9/8/2014 Initial content copied verbatim from.
ECE 103 Engineering Programming Chapter 10 Variables, AKA Objects Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CS 162 Introduction to Computer Science Chapter 17 C++ String Objects Herbert G. Mayer, PSU (Copied from Prof. Phillip Wong at PSU) Status 11/30/2014.
ECE 103 Engineering Programming Chapter 48 Typedef and Enum Type Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
CS 161 Introduction to Programming and Problem Solving Chapter 19 Single-Dimensional Arrays Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
ECE 103 Engineering Programming Chapter 41 C Pointers, Part 3 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
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.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.
C LANGUAGE UNIT 3. UNIT 3 Arrays Arrays – The concept of array – Defining arrays – Initializing arrays.
Computer Organization and Design Pointers, Arrays and Strings in C
Strings CSCI 112: Programming in C.
INC 161 , CPE 100 Computer Programming
Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
Lecture-5 Arrays.
Module 2 Arrays and strings – example programs.
Strings A string is a sequence of characters treated as a group
Computer Science 210 Computer Organization
Arrays in C.
Programming Languages and Paradigms
Lecture 11: Strings B Burlingame 11 April 2018.
Lecture 10: Strings B Burlingame 4 April 2018.
CS111 Computer Programming
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Lecture 8b: Strings BJ Furman 15OCT2012.
C Stuff CS 2308.
INC 161 , CPE 100 Computer Programming
CS 106 Computing Fundamentals II Chapter 71 “Indexing”
Week 9 – Lesson 1 Arrays – Character Strings
Arrays and Pointers Reference: Chapter , 4.11 CMSC 202.
Strings A collection of characters taken as a set:
C-strings In general, a string is a series of characters treated as a unit. Practically all string implementations treat a string as a variable-length.
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
ECE 103 Engineering Programming Chapter 32 Array Parameters
Chapter 16 Pointers and Arrays
Strings What is a string? It is an array of characters terminated with
Chapter 8 Character Arrays and Strings
CPS120: Introduction to Computer Science
ECE 103 Engineering Programming Chapter 19 Nested Loops
ECE 103 Engineering Programming Chapter 12 More C Statements
ECE 103 Engineering Programming Chapter 51 Random Numbers
String manipulation string.h library
ECE 103 Engineering Programming Chapter 46 argc, argv, envp
ECE 103 Engineering Programming Chapter 37 C Macro Parameters
ECE 103 Engineering Programming Chapter 8 Data Types and Constants
Variables, Pointers, and Arrays
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
CS250 Introduction to Computer Science II
Exercise Arrays.
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
Lecture 19: Working with strings
Strings #include <stdio.h>
CS31 Discussion 1D Winter19: week 4
CS31 Discussion 1H Fall18: week 6
Pointers, Dynamic Data, and Reference Types
cout << str1;  pear
Presentation transcript:

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 Wong @ PSU ECE

Syllabus String Data Simple String Operations Examples

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

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

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

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

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

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

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

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

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