Lecture 10: Strings B Burlingame 4 April 2018.

Slides:



Advertisements
Similar presentations
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Advertisements

Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
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.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Current Assignments Homework 5 will be available tomorrow and is due on Sunday. Arrays and Pointers Project 2 due tonight by midnight. Exam 2 on Monday.
Chapter 10.
1 9/10/07CS150 Introduction to Computer Science 1 Data Types Section 2.7 – 2.12 CS 150 Introduction to Computer Science I.
CS150 Introduction to Computer Science 1
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
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.
Lecture 12: Pointers B Burlingame 25 Nov Announcements Homework 6 due Homework 7 posted, due with the final  Final prep Take home Lab posted tonight.
Lecture 11: Files & Arrays B Burlingame 18 November 2015.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
A First Book of ANSI C Fourth Edition Chapter 9 Character Strings.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
Lecture 6: More Decisions & Arrays B Burlingame 9 March 2016.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Lecture 11: Pointers B Burlingame 13 Apr Announcements Rest of semester  Homework Remaining homework can be done in pairs, turn in one paper with.
Chapter 2 Array and String. Array Definition of Array : An array is a sequence or collection of the related data items that share a common name. Purpose.
Lecture 5: How to think like a programmer, Arrays & Strings
Computer Organization and Design Pointers, Arrays and Strings in C
EGR 2261 Unit 10 Two-dimensional Arrays
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
Characters and Strings
Fundamentals of Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
C Programming Tutorial – Part I
Programming Languages and Paradigms
Quiz 11/15/16 – C functions, arrays and strings
CSE 303 Concepts and Tools for Software Development
Lecture 9: Pointers B Burlingame 25 October 2017.
A First Book of ANSI C Fourth Edition
CSE 303 Lecture 14 Strings in C
Programming Paradigms
Module 2 Arrays and strings – example programs.
Strings A string is a sequence of characters treated as a group
Arrays in C.
Programming Languages and Paradigms
Lecture 11: Strings B Burlingame 11 April 2018.
CSCI206 - Computer Organization & Programming
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
5. Arrays, Pointers and Strings
Lecture 8b: Strings BJ Furman 15OCT2012.
EKT150 : Computer Programming
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.
Arrays Chapter 8 Copyright © 2008 W. W. Norton & Company.
Chapter 2 Array and String Visit to more Learning Resources.
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
Arrays Arrays A few types Structures of related data items
Arrays.
C++ Programming Lecture 20 Strings
Introduction to C EECS May 2019.
Strings #include <stdio.h>
Programming Languages and Paradigms
Characters and Strings Functions
CS31 Discussion 1H Fall18: week 6
Presentation transcript:

Lecture 10: Strings B Burlingame 4 April 2018

Announcements Midterm due next week Read chapter 9 You may work on your midterm projects in lab, this week. No new lab will be assigned No group discussion and only one person per PC Remember: the midterm must be individual work. Any duplicate work will be harshly dealt with.

Recall: What is an Array? So far we've dealt with scalar variables contain just one value Arrays are collections of data of the same type that occupy contiguous memory locations Individual values in the collection are called elements of the array Need to declare before use: #include <stdio.h> int main() { int i=0; double test[4] = {0}; test[0]=95.5; test[1]=74.0; test[2]=88.5; test[3]=91.0; for(i=0; i<4; i++) printf("test[%d]=%lf",i,test[i]); printf(" @ 0x%p\n",&test[i]); } return 0; Run array_practice2.c in ChIDE. Format (1D array) type array_name [num_elements]; Ex. Array of 4 doubles named, 'test'

What is an array? - 2 int nums [10]; 10 element array of integers Element no. 3 is accessed by: nums [2] because indexing begins at 0

Accessing Array Elements #include <stdio.h> int main() { int i=0; doube test[4]={0}; test[0]=95.5; test[1]=74.0; test[2]=82.75; test[3]=91.5; for(i=0; i<4; i++) printf("test[%d]=%lf",i,test[i]); printf(" @ 0x%p\n",&test[i]); } return 0; Individual elements of an array are accessed by their index number ** Important Note** Array indexing starts at zero (take note of this, it is easy to forget!) char test[4]; the first element is test [0] the fourth element is test [3] Be careful! The C compiler may not stop you from indexing beyond the boundary of your array. (What could happen?) What is test[4]? array_practice2.c

Initializing Array Elements /* array_practice3.c */ #include <stdio.h> int main() { int i=0; int nums[5]={0,1,2,3,4}; for(i=0; i<5; i++) printf("nums[%d]=%d",i,nums[i]); printf(" @ 0x%p\n",&nums[i]); } return 0; Use braces to enclose the elements Separate elements by commas Can set number of elements in declaration: Explicit: int nums[5] Implicit: int imp[ ] = {1, 2}; Run array_practice3.c in ChIDE. Point out addresses for int array elements. Four bytes each! Run array_practice4.c in ChIDE to show my version of keyboard entry.

Initializing Array Elements /* array_practice3.c */ #include <stdio.h> const int BIG_INDEX = 5000; int main() { int i=0; int nums[BIG_INDEX]={0}; for(i=0; i < BIG_INDEX; i++) nums[i] = 34; } return 0; For long, non-zero initializations, for loop is usually more efficient to program Note the use of a variable index Run array_practice3.c in ChIDE. Point out addresses for int array elements. Four bytes each! Run array_practice4.c in ChIDE to show my version of keyboard entry.

Strings Strings are encoded arrays of characters designed to hold language Recall that all characters are encoded as numeric values Most modern systems use a variant of ASCII or Unicode We’ll assume ASCII

ASCII Table

Strings By convention, strings in C are a null terminated array of characters There is no intrinsic string datatype Null equals character value 0 i.e. char z = 0; z has a null character written \0 Declaration: char string[] = “Hello world”; Stored: Hello World\0 (ie 12 characters) All string handling expects this

Working with strings Much like working with pointers and arrays Many helper routines in string.h strcmp – tests strings for equivalence strcat – concatenates two strings strstr – locates a string within a string Etc. http://www.cplusplus.com/reference/cstring/ char string[] = “Hello World”; for( int i = 0; string[i] != 0 ; ++i ) { if( string[i] >= ‘A’ && string[i] <= ‘Z’ ) string[i] = string[i] + (97 – 65); }

Formatted Input (1) We use two commands to properly obtain input from the keyboard fgets & sscanf fgets places data collected form the keyboard into a buffer stored as a character string up to and including a carriage return (when the user presses return or enter) fgets( buffer, sizeof(buffer), stdin );

Formatted Input (2) sscanf splits the input into variables Space delimited Uses conversion metasymbols like printf sscanf( buffer, “%d %f”, &x, &y); Note the ambersands (&) & is commonly required for input & is not required for strings sscanf( buffer, “%s %s”, &city, &state);

Formatted Input (3) #include <stdio.h> #define BUFF_SIZE 100 //const int BUFF_SIZE = 100; int main( void ) { float x = 0; float y = 0; char buffer[BUFF_SIZE] = { 0 }; printf( "Enter two floats(x y): "); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%f %f", &x, &y );

Formatted Input (4) #include <stdio.h> #define BUFF_SIZE 100 #define NUM_SIZE 100 int main( void ) { float x[NUM_SIZE] = { 0 }; char buffer[BUFF_SIZE] = { 0 }; for( int i=0; i < NUM_SIZE; ++i ) printf("Enter entry %d: ", i ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%f", &x[i]); }

Formatted Input (5) #include <stdio.h> #define BUFF_SIZE 100 #define NAME_SIZE 100 int main( void ) { char name[NAME_SIZE] = { 0 }; char buffer[BUFF_SIZE] = { 0 }; printf("Enter name:” ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%s", name); // note, no &

Formatted Input (5) #include <stdio.h> #define BUFF_SIZE 100 #define NAME_SIZE 100 #define NAME_QTY 5 int main( void ) { char names[NAME_QTY][NAME_SIZE] = { 0 }; char buffer[BUFF_SIZE] = { 0 }; for( int i=0; i < NAME_QTY; ++i ) printf("Enter entry %d: ", i ); fgets( buffer, sizeof(buffer), stdin ); sscanf( buffer, "%s", names[i]); }

http://www.cplusplus.com/cstring/strcmp

Comparing strings #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { char name[100] = "Falken"; // String literal initialization char input_name[100] = ""; // Empty string initialization char buff[100] = { 0 }; // All null initialization fgets(buff, sizeof(input_name), stdin); sscanf(buff, "%s", input_name); // Note the lack of & if (strcmp(input_name, name) == 0) // strcmp is in string.h, unusually { // strcmp returns <0, 0, or >0. // 0 means "no difference" printf("Hello Professor %s\n", input_name); } else printf("Hello %s, would you like to play a game?\n", input_name); return(EXIT_SUCCESS);

References Darnell, P. A. & Margolis, P. E. (1996) C, a software engineering approach, 3rd ed., Springer, New York, p. 327. http://www.cppreference.com/wiki/c/io/fopen, Visited 23OCT2010.