尋隱者不遇 松下問童子, 言師採藥去。 只在此山中, 雲深不知處。 ~ 賈島 1. C Time Library ctime  Types clock_t - Clock type clock_t size_t - Unsigned integral type size_t time_t - Time.

Slides:



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

 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 sprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
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 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.
C programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
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.
Topic 10 - Strings.
1 C-strings String = null-terminated array of characters The null character ('\0') specifies where the string terminates in memory. Example: The string.
CS 192 Lecture 11 Winter 2003 December 29-30, 2003 Dr. Shafay Shamail.
Friday, January 05, 2007 A few weeks of developing and testing can save a whole afternoon in the library. -Anonymous.
CS Nov 2006 C-strings.
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. Sentences in English are implemented as strings in the C language. Computations involving strings are very common. E.g. – Is string_1 the same.
Data Structure and C Part-6. Naming a Function Any valid variable-name can be given to the user-defined function. The main program itself is considered.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
1 Chapter 10 Characters, Strings, and the string class.
Introduction to Programming Strings. 2 Introduction  Until now  We have seen strings in cout  Our old definition: string is a set of char between “”
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT8: Characters and Strings CS2311 Computer Programming.
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
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.
Character Arrays Based on the original work by Dr. Roger deBry Version 1.0.
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
String functions+ string I.Mona Alshehri. String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of.
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.
Chapter 8: Character and String CMPD144: Programming 1.
Spring 2005, Gülcihan Özdemir Dağ Lecture 8, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 8 Outline 8.1 Declaring.
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:
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.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 5 - Pointers and Strings Outline 5.1 Introduction 5.2 Pointer Variable Declarations and Initialization.
 2003 Prentice Hall, Inc. All rights reserved. 11 Fundamentals of Characters and Strings Character constant –Integer value of a character –Single quotes.
Chapter 8 Characters and Strings. Objectives In this chapter, you will learn: –To be able to use the functions of the character handling library ( ctype).
C++ Programming Lecture 19 Strings The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
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.
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.
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.
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.
String in C++. 2 Using Strings in C++ Programs String library or provides functions to: - manipulate strings - compare strings - search strings ASCII.
EC-111 Algorithms & Computing Lecture #10 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
INC 161 , CPE 100 Computer Programming
Characters, C-Strings, and More About the string Class
CSE 303 Lecture 14 Strings in C
Standard Version of Starting Out with C++, 4th Edition
Standard Version of Starting Out with C++, 4th Edition
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C++ Programming Lecture 20 Strings
Strings Adapted from Dr. Mary Eberlein, UT Austin.
閨怨 ~王昌齡 閨中少婦不知愁, 春日凝妝上翠樓; 忽見陌頭楊柳色, 悔教夫婿覓封侯。.
C Characters and Strings
閨怨 ~王昌齡 閨中少婦不知愁, 春日凝妝上翠樓; 忽見陌頭楊柳色, 悔教夫婿覓封侯。.
The std::string class.
Presentation transcript:

尋隱者不遇 松下問童子, 言師採藥去。 只在此山中, 雲深不知處。 ~ 賈島 1

C Time Library ctime  Types clock_t - Clock type clock_t size_t - Unsigned integral type size_t time_t - Time type time_t struct tm - Time structure (See Chapter 7) struct tm  Time manipulation clock - Ticks since the program was launched clock time - Get current time time mktime - Convert tm structure to time_t mktime  Macro CLOCKS_PER_SEC - Clock ticks per second CLOCKS_PER_SEC  Conversion asctime - Convert tm structure to string asctime ctime - Convert time_t value to string ctime gmtime - Convert time_t to tm as UTC time gmtime localtime - Convert time_t to tm as local time localtime strftime - Format time as string strftime 2

time() /* time example */ #include int main () { time_t seconds; seconds = time(NULL); std::cout << seconds/3600 << " hours since" << " January 1, 1970\n"; return 0; } /* time example */ #include int main () { time_t seconds; time(&seconds); std::cout << seconds/3600 << " hours since" << " January 1, 1970\n"; return 0; } 3 #include time_t time ( time_t * timer );

ctime()  Converts the time_t object (seconds since ) to a C string containing a human-readable version of the corresponding local time and date. /* ctime example */ #include int main () { time_t rawtime; time ( &rawtime ); std::cout << "The current local time is: " << ctime (&rawtime) << std::endl; return 0; } 4 #include char * ctime ( const time_t * timer ); The current local time is: Sat Nov 10 11:57: output

5 String Manipulation

6 Standard Header                   Q: What’s the difference between and ? A: In C++, we include ; in C, we include. Actually, including the standard header will effectively include the standard C library header within the std namespace.std namespace

7 strlen()  Find length of string. #include using std::cout; using std::endl; int main() { char str[10]="Hello"; cout << sizeof str; cout << strlen(str); } #include size_t strlen( char *str ); 10 5 output

8 strchr()  Find first occurrence of specified character in string. #include char *strchr( const char *str, int ch ); char str1[] = "Hello"; char* r= strchr(str1, 108); cout << r << endl; llo output

9 strstr()  Find first occurrence of specified string in another string. char str1[] = "Hello Hello"; char str2[] = "el"; char* r = strstr(str1, str2); cout << r << endl; #include char *strstr( const char *str1, const char *str2 ); ello Hello output

10 strcmp()  Compare two strings. #include int strcmp( const char *str1, const char *str2 ); char str1[] = "Hello"; char str2[] = "He"; int r= strcmp(str1, str2); cout << r << endl; output 1 char str1[] = "Hello"; char str2[] = "Hello"; int r= strcmp(str1, str2); cout << r << endl; output 0 char str1[] = "Hello"; char str2[] = "Hello!!"; int r= strcmp(str1, str2); cout << r << endl; output a positive number a negative number

11 strcpy()  Copy str2 to str1, including its terminating null character. #include char *strcpy(char *str1, const char *str2); char str1[] = "Hello"; char str2[] = "abc"; strcpy(str1, str2); cout << str1 << endl; output abc

12 strncpy()  Copy at most n characters of str2 to str1, not including its terminating null character. #include char *strncpy( char *str1, const char *str2, size_t n); char str1[] = "Hello"; char str2[] = "abc"; strncpy(str1, str2, 2); cout << str1 << endl; output abllo

13 strcat()  Append one string to another. #include char *strcat( char *str1, const char *str2 ); char str1[10] = "Hello"; char str2[] = "!!"; strcat(str1, str2); cout << str1 << endl; output Hello!!

14 Reference  Standard C Library ex.html ex.html MSDN – String ManipulationString Manipulation

Exercise  Design a function int lower_string(char* str) to convert every lowercase letters in a string to uppercase. For example, char str[] = "WoW"; lower_string(str); cout << str << endl;  The result will be "wow"  You may also utilize the tolower() function defined in C library ctype. 15

Exercise  Write a function int find(char* text, char* pattern), which can count the occurrence of pattern in text. For example, find("This is an island.", "is") == 3 find("Hello", "g") == 0  Test your function with this main program.main program  Hint: You may utilize the strstr() function, or define a new function by yourself. 16