Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving.

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.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
 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.
Ch 8. Characters and Strings Timothy Budd 2 Characters and Literals Strings Char in C++ is normally an 8-bit quantity, whereas in Java it is a 16-bit.
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
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 9 Strings Instructor: Alkar / Demirer. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Chapter 9 Character Strings
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 8 - Characters and Strings Outline 8.1Introduction.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Introduction to Computers and Programming Class 22 Character Arrays (Strings) Professor Avi Rosenfeld.
CS1061 C Programming Lecture 14: Strings A. O’Riordan, 2004.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
CS Nov 2006 C-strings.
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
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 5 Arrays and Strings C Programming © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
February 14, 2005 Characters, Strings and the String Class.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
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)
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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’};
Characters, Strings, And The string Class Chapter 10.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
More Pointers and Arrays Kernighan/Ritchie: Kelley/Pohl: Chapter 5 Chapter 6.
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.
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.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
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.
Characters and Strings
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.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
C Characters and Strings
Lecture 8 String 1. Concept of strings String and pointers
CSE 303 Lecture 14 Strings in C
Chapter 8 - Characters and Strings
INC 161 , CPE 100 Computer Programming
CprE 185: Intro to Problem Solving (using C)
EECE.2160 ECE Application Programming
Strings What is a string? It is an array of characters terminated with
Lec 11.
C Characters and Strings
EECE.2160 ECE Application Programming
Introduction to Problem Solving and Programming
Presentation transcript:

Slides from Shane Griffith (TA and guest instructor in Fall 2008) CprE 185: Intro to Problem Solving (using C)

Strings CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © 2008

Chapter 9 (Strings)

Outline Declare a String Input a String Assign a String String functions Character type operations

Print a character Array Program: // character array char c[10] = {‘E’, ‘E’, ‘/’,‘C’, ‘p’, ‘r’,‘E’}; for(i=0; i< 7; i++) { printf(“%c“, c[i]); } ____________________________________________ Output: EE/CprE

Print another way Program: // character array char c[10] = {‘E’, ‘E’, ‘/’,‘C’, ‘p’, ‘r’,‘E’}; printf(“%s”, c); //%s to print a string ____________________________________________ Output: EE/CprE

Declare another way Program: // define character array with a string char c[10] = “EE/CprE”; printf(“%s”, c); //%s to print a string ____________________________________________ Output: EE/CprE

Character vs. String Do NOT confuse strings with individual characters ‘E’ is a character “E” is a string initialize a character array with 1) Array of characters {‘E’,‘E’,‘/’,‘C’,‘p’,‘r’,‘E’} 2)A String “EE/CprE” //easier to type

Declaring strings char c[10] = “EE/CprE”; //only 7 chars What about the uninitialized characters in the string?

Array contents Contents of the c array c E E / C p r E \0 ? ? NULL character automatically assigned

Array Contents Program: // character array char c[10] = “EE/CprE”; printf(“%s”, c); //output 1 c[8] = ‘a’; printf(“%s”, c); //output 2 _________________________ Output 1: EE/CprE Output 2: EE/CprE c E E / C p r E \0 a ?

Strings, what’s happening Prints until NULL character is reached  leave room for it! Program: // array size should be >= 8 char c[7] = “EE/CprE”; printf(“%s”, c); //%s to print a string ____________________________________________ Output:

Strings Length is determined by first NULL in the string Most string functions in C add NULL automatically Array of strings is a double array of characters  From the book: char month[12][10] = {“January”, “February”, …, “December”};

Program: char c[N]; scanf(“%s”, c); //no & symbol is required printf(“%s”, c); ____________________________________________ Input: “EE CprE” Output: EE //input separated by white space Input a String

char c[N]; gets(c); printf(“%s\n”, c); ____________________________________________ Input: “EE CprE” Output: EE CprE Input a String gets  Get a string from user input  reads until enter is pressed

Program: // character array char c[N]; c[N] = “Monday”; //will NOT work Assign value to a String Cannot use = operator in C to assign a String

#include … char c[N]; char tomorrow[] = “Tuesday”; … strcpy(c, “Monday”); //c is the destination … strcpy(c, tomorrow); //another assignment Assign value to a String Use the String function strcpy in string.h  Copies a string into a destination string

#include … char c[7]; strcpy(c, “Wednesday”); Assign value to a String Watch out for overflow (bad) c W e d n e s d a \0 y overflow a y

#include … char c[7]; strncpy(c, “Wednesday”, 7); Assign value to a String Better to use strncpy  copies up to n characters from the source c W e d n e s d need NULL

#include … char c[7]; strncpy(c, “Wednesday”, 6); c[6] = ‘\0’; //OR c[6] = NULL; //NULL and ‘\0’ are the same Assign value to a String Better to use strncpy  assign NULL to the end afterword c W e d n e s \0

String Functions strcmp or strncmp  Compares two strings (good for sorting) strcmp(“Saturday”, “Sunday”); //answer is -1 strncmp(“way”, “wash”, 2); //answer is 0 strlen  Returns the number of characters in “Saturday” strlen(“Saturday”) //answer is 8

String Functions strcat  Concatenate two strings (good for sorting) char a[N] = “Hello ”; char b[N] = “World!\n”; strcat(a, b); printf(“%s”, a); ____________________________________________ Output: Hello World!

Character Operations isalpha  is the character a letter of the alphabet? isdigit  Is the character a number? islower, isupper  Checks the case of the letter ispunct isspace

A good reference library- functions.html#String%20library%20functionshttp:// library- functions.html#String%20library%20functions

Questions?

THE END