Introduction to Programming Strings. 2 Introduction  Until now  We have seen strings in cout  Our old definition: string is a set of char between “”

Slides:



Advertisements
Similar presentations
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
Advertisements

Strings Input/Output scanf and printf sscanf and sprintf gets and puts.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
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.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
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.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
C programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
Lesson 10 Characters, C-Strings, and the string Class CS1 Lesson John Cole1.
Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
1 CS 201 String Debzani Deb. 2 Distinction Between Characters and Strings When using strcat, one may be tempted to supply a single character as one of.
Introduction to C Programming CE Lecture 13 Strings in C.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
CS161 Introduction to Computer Science Character Data.
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 12 More.
Strings. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
PRESENTED BY: ER. SUMANPREET KAUR LECTURER IN CE DEPTT. GPCG ASR.
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.
1 Chapter 10 Characters, Strings, and the string class.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: More on C-Strings and the string Class Starting Out with.
Two-week ISTE workshop on Effective teaching/learning of computer programming Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 13 Strings (Continued)
Chapter 10. Characters, Strings and the string class Csc 125 Introduction to C++ Fall 2005.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10: Characters, Strings, and the string class.
Characters, Strings, And The string Class Chapter 10.
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 6 Array and String.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 10: Characters, C- Strings, and More About.
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:
CSC 270 – Survey of Programming Languages
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
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. C Time Library ctime  Types clock_t - Clock type clock_t size_t - Unsigned integral type size_t time_t - Time.
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];
Chapter Characters, Strings, and the string class 10.
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
Today’s Material Strings Definition Representation Initialization
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Strings, and the string Class. C-Strings C-string: sequence of characters stored in adjacent memory locations and terminated by NULL character The C-string.
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.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
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.
CMPE Data Structures and Algorithms in C++ September 7 Class Meeting
Lecture 8 String 1. Concept of strings String and pointers
Characters, C-Strings, and More About the string Class
CSE 303 Lecture 14 Strings in C
Strings A string is a sequence of characters treated as a group
Chapter 12: More on C-Strings and the string Class
Standard Version of Starting Out with C++, 4th Edition
CS111 Computer Programming
10.1 Character Testing.
Lecture 11 Strings.
CS 144 Advanced C++ Programming February 7 Class Meeting
Standard Version of Starting Out with C++, 4th Edition
EECE.2160 ECE Application Programming
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C Characters and Strings
Presentation transcript:

Introduction to Programming Strings

2 Introduction  Until now  We have seen strings in cout  Our old definition: string is a set of char between “” cout<<“This is a string”;  Strings:  An array of chars  Terminated by the null char '\0'

3 Strings in C  Since strings are array char str3[] = {'p', 'r', 'o', 'g', 'r', 'a', 'm', '\0'}; char str1[8] = "program"; char str2[] = "program";

4 Reading & Writing Strings  cout can be used to print strings cout<<“program”;  scanf can be used to read strings char str[200]; cin>>str;  Initial white spaces are ignored  Read until space or '\n' (which is replaced by \0)  We must allocate sufficient size

5 String Library  Access to string library by #include  Many functions to work with strings  Find the length of string  Compare strings  Copy strings  Search in strings  …

6 Length of String  strlen(str) : Length of string  From start to first occurrence of the null char char str[] = "This is test"; char str1[10]={'a', 'b', '\0', 'c', '\0'}; strlen(str)  12 strlen(str1)  2

7 Compare Strings  str1 and str2 are compared as follows  Compare char by char from left to right until str1 and str2 has same chars.  In the first different char  If(char of str1 < char of str2)  str1 < str2  If (both string finish)  str1 = str2  strcmp(str1, str2): compare str1 and str2  If(str1 == str2)  return 0  If(str1 < str2)  return -1  If(str1 > str2)  return 1

8 Compare Strings: Examples char s1[] = "abc"; char s2[] = "abc"; i = strcmp(s1, s2);//i = 0 char s3[] = "abc"; char s4[] = "abx"; i = strcmp(s3, s4);//i = -1 char s5[] = "axc"; char s6[] = "abc"; i = strcmp(s5, s6);//i = 1 char s7[] = "ab"; char s8[] = "abc"; i = strcmp(s7, s8); //i = -1 char s9[] = "abc"; char s10[] = "aBc"; i = strcmp(s9, s10); //i = 1

Compare Strings  strcmpi(str1, str2)  Compares str1 and str2 similar to strcmp  But ignores uppercase/lowercase difference char str1[]="ABC", str2[]="abC"; strcmpi(str1, str2)  0 9

10 Copy Strings  Strings should be copied char by char  strcpy(dst_str, src_str) : copy the src_str to the dst_str  src_str is a constant string  dst_str should have sufficient size

11 Copy Strings: Example char str1[] = "Test String"; char str2[20]; strcpy(str2, str1); cout<<str2<<endl; cout<<str1<<endl; Test String

12 Concatenate Strings  strcat(dst, src) : Append the src string to the end of dst  src is constant string  dst should have sufficient space

13 Concatenate Strings: Example char str1[] = "String"; char str2[20]= "Test "; strcat(str2, str1); cout<<str2; Test String

14 Sized Version of the Functions  strncpy(dst, src, n) :  copys n chars from src to dst  If(strlen(src) > n)  Copies n chars to dst  Does not add ‘\0’ to end of dst  If(strlen(src) < n)  Copy src to dst  Add n – strlen(src) – 1 ‘\0’ to end of dst  dst must be large enough  n < size of dst

15 Sized Version of Functions  strncmp(str1, str2, n) :  compares the first x chars  x = min{n, strlen(str1)+1, strlen(str2)+1}  strncat(dst, src, n) :  Appends the x chars from src to dst  x = min{n, strlen(src)}  Adds ‘\0’ to end of dst  dst must be large enough

16 String as Array  Strings are array of chars  We work on arrays element by element  We can work on strings char by char char str1[] = "100000"; str1[2] = '2';  We can pass strings to functions

17 Array of Strings  2 dimensional array, each row is a string

cctype  Many function to work on chars  Check digit  Check alphabetic  Check lower or upper case  Convert from/to upper/lower case

cctype  int isdigit(ch)  Check ch is digital char or not  int isalpha(ch)  Check ch is alphabetic or not  int islower(ch)  Check ch is lowercase alphabetic or not  int isupper(ch)  Check ch is uppercase alphabetic or not  char tolower(ch)  Convert ch to lowercase and return it  char toupper(ch)  Convert ch to upper case and return it