Lecture 11: Strings B Burlingame 11 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

Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
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.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
Lecture 3 August 31 Chapter 3. Chapter 3 – numbers, string, booleans integer: MATLAB stores numeric data as double-precision floating point (double) by.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
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.
CNG 140 C Programming (Lecture set 9) Spring Chapter 9 Character Strings.
Lecture 8a: File I/O BJ Furman 21MAR2011. Learning Objectives Explain what is meant by a data stream Explain the concept of a ‘file’ Open and close files.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
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.
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
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.
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.
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 5 : September 4.
Lecture 6: More Decisions & Arrays B Burlingame 9 March 2016.
Lecture 11: Pointers B Burlingame 13 Apr Announcements Rest of semester  Homework Remaining homework can be done in pairs, turn in one paper with.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
Lecture 5: How to think like a programmer, Arrays & Strings
Computer Organization and Design Pointers, Arrays and Strings in C
INC 161 , CPE 100 Computer Programming
Chapter 6: Data Types Lectures # 10.
Lecture 8 String 1. Concept of strings String and pointers
Programming Languages and Paradigms
Lecture-5 Arrays.
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.
Computer science C programming language Lesson 5
Arrays in C.
Programming Languages and Paradigms
Lecture 10: Strings B Burlingame 4 April 2018.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
5. Arrays, Pointers and Strings
Lecture 8b: Strings BJ Furman 15OCT2012.
C Stuff CS 2308.
CSI 121 Structured Programming Language Lecture 21 Character Strings
Chapter 9 - Arrays Outline 6.1 Introduction 6.2 Arrays
INC 161 , CPE 100 Computer Programming
Data Structures and Programming Techniques
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.
Chapter 2 Array and String Visit to more Learning Resources.
Chapter 8 Character Arrays and Strings
CPS120: Introduction to Computer Science
Line at a time I/O with fgets() and fputs()
C Strings Prabhat Kumar Padhy
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
EECE.2160 ECE Application Programming
Exercise Arrays.
Arrays C provides the option to the user to combine similar data types into a single entity It followed contiguous.
Arrays.
Lecture 19: Working with strings
Character Arrays char string1[] = “first”;
Strings #include <stdio.h>
Programming Languages and Paradigms
ㅎㅎ Sixth step for Learning C++ Programming Pointer new, delete String
Characters and Strings Functions
Presentation transcript:

Lecture 11: Strings B Burlingame 11 April 2018

Announcements Midterm due Read chapter 9 Homework #7 due next week Sign up for review sessions over the next week Read chapter 9 Homework #7 due next week

Plan for today Review strings Demonstrate an approach to solving Happy Numbers

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'

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

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); }

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.