16.216 ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.

Slides:



Advertisements
Similar presentations
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
Advertisements

Strings string.h library. String Library Functions Dr. Sadık EşmelioğluCENG 1142 NameDescription strlen return the length of string not counting \0 strcopy.
 2003 Prentice Hall, Inc. All rights reserved Fundamentals of Characters and Strings Character constant –Integer value represented as character.
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 09 Strings, IDEs. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 29, 2002.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() 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.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
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 Fourteen Strings Revisited. Strings A string is an array of characters A string is a pointer to a sequence of characters A string is a complete.
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
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.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
C-Strings Joe Meehean. C-style Strings String literals (e.g., “foo”) in C++ are stored as const char[] C-style strings characters (e.g., ‘f’) are stored.
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.
Introduction to C programming
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.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
APS105 Strings. C String storage We have used strings in printf format strings –Ex: printf(“Hello world\n”); “Hello world\n” is a string (of characters)
 2008 Pearson Education, Inc. All rights reserved Pointers and Pointer-Based Strings.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
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.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
String Array (Multidimensional Arrays) 1. A string array is a multidimensional array of strings. It is declared in the following syntax: char variable_name[No_of_strings][size_of_each_string];
5.6 String Processing Part 2. Sprintf(destnvar,…..regularprintf) Write formatted data to string Same as printf except the output is put in variable. A.
Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
Built-in Functions for NTCAs. strlen char array[10] = “Hello”; int length = strlen(array); cout
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
19-Feb-02 Sudeshna Sarkar, CSE, IIT Kharagpur1 Arrays, Pointers, Strings Lecture 18 19/2/2002.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Dale Roberts, Lecturer Computer Science,
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
ECE Application Programming
ECE Application Programming
ECE Application Programming
Lecture 8 String 1. Concept of strings String and pointers
ECE Application Programming
INC 161 , CPE 100 Computer Programming
Lecture 11 Strings.
EECE.2160 ECE Application Programming
Strings What is a string? It is an array of characters terminated with
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Presentation transcript:

ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5

Lecture outline Announcements/reminders  Program 7 due 4/20  Program 8 to be posted by end of week  Grading on previous programs to be completed ASAP Today  Review strings and character arrays  PE5 Can download “starter” code from website 4/17/2015 ECE Application Programming: Lecture 31 2

Review: strings Represented as character arrays Can be initialized using string constants  char hello[] = “Hello”; Can access individual elements  hello[3] = ‘l’; Can print directly or with formatting  Print directly: printf(hello);  Print w/formatting using %s: printf(“%s\n”, hello); Must leave enough room for terminating ‘\0’ 4/17/2015 ECE Application Programming: Lecture 31 3

Review: String functions In library:  Copying strings: char *strcpy(char *dest, const char *source); char *strncpy(char *dest, const char *source, size_t num); Return dest  Comparing strings: int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t num); Character-by-character comparison of character values Returns 0 if s1 == s2, 1 if s1 > s2, -1 if s1 < s2 4/17/2015 ECE Application Programming: Lecture 31 4

Review: String functions (cont.) Find # of characters in a string  size_t strlen(const char *s1);  Returns # characters before ‘\0’ Not necessarily size of array “Add” strings together—string concatenation  char *strcat(char *dest, const char *source);  char *strncat(char *dest, const char *source, size_t num);  Returns dest 4/17/2015 ECE Application Programming: Lecture 31 5

Example: Strings What does the following program print? int main() { char s1[15]; int n1; char s2[10] = “.216”; int n; strncpy(s1, “16”, 15); n1 = strlen(s1); printf(“s1 = %s\n”, s1); printf(“Length of s1 = %d\n\n”, n1); printf(“%c\n\n”, s1[1]); strncat(s1,s2,10); n1 = strlen(s1); printf(“s1 = %s\n”, s1); printf(“Length of s1 = %d\n\n”, n1); // Assume user inputs: ABC ABD printf(“Enter two strings:”); scanf(“%s%s”, s1, s2); n = strncmp(s1, s2, 15); if (n > 0) printf(“%s > %s\n”, s1, s2); else if (n < 0) printf(“%s < %s\n”, s1, s2); else printf(“%s == %s\n”, s1, s2); return 0; } 4/17/2015 ECE Application Programming: Lecture 31 6

Example solution s1 = 16  Initial value of s1 Length of s1 = 2 6  s1[1] s1 =  s1 after strncat() Length of s1 = 6 Enter two strings: ABC ABD ABC < ABD  Result of strncmp() 4/17/2015 ECE Application Programming: Lecture 31 7

Example: Using string functions Works with main program in PE Assume input strings have max of 49 chars (+ ‘\0’) Write a function to do each of the following:  int readStrings(char *s); Repeatedly read strings from standard input until the input string matches s. Return the number of strings read.  void copyNull(char *s1, char *s2, int n); Copy the first n characters of s2 into s1, and make sure that the new version of s1 terminates with a null character.  int fillString(char *s, int n); Repeatedly read strings from standard input and concatenate them to s until there is no room in the string. Return the final length of the string.  For example, if s is a 6-character array already holding “abcd”:  User enters “e”—string is full; return 5  User enters “ef”—there’s not enough room; return 4 Assume s initially contains null terminated string (or is empty) 4/17/2015 ECE Application Programming: Lecture 31 8

Example solution int readStrings(char *s) { char str[50];// Assume max 50 chars // (not specified) int count = 0; do { scanf(“%s”, str);// NOTE: do not // need &str count++; } while (strcmp(str, s) != 0); return count; } 4/17/2015 ECE Application Programming: Lecture 31 9

Example solution (cont.) void copyNull(char *s1, char *s2, int n) { strncpy(s1, s2, n); s1[n] = ‘\0’; } 4/17/2015 ECE Application Programming: Lecture 31 10

Example solution (cont.) int fillString(char *s, int n) { char input[50];// Assume max // 50 chars int charsLeft;// Space remaining // in s do { scanf(“%s”, input); // Calculate # chars left in array if input // string is added. Need to leave room for ‘\0’ charsLeft = n – (strlen(s) – 1) – strlen(input); if (charsLeft > 0)// Enough space to add this string strcat(s, input);// and continue else {// Out of room if (charsLeft == 0)// Can add input, but then out of room strcat(s, input); return strlen(s); } } while (1); } 4/17/2015 ECE Application Programming: Lecture 31 11

Next time 2-dimensional arrays May also start file I/O 4/17/2015 ECE Application Programming: Lecture 31 12