Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

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.
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.
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.
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.
C programming---String. String Literals A string literal is a sequence of characters enclosed within double quotes: “hello world”
 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.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
 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.
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.
 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.
1 Chapter 10 Characters, Strings, and the string class.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
EPSII 59:006 Spring Introduction Fundamentals of Strings and Characters Character Handling Library String Conversion Functions Standard Input/Output.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Introduction to C programming
C How to Program, 6/e © by Pearson Education, Inc. All Rights Reserved.
CHAPTER 8 CHARACTER AND STRINGS
 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.
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.
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)
Copyright © 2012 Pearson Education, Inc. Chapter 10: Characters, C- Strings, and More About the string Class.
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’};
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.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI C-Style Strings Strings and String Functions Dale Roberts, Lecturer.
Characters, Strings, And The string Class Chapter 10.
1 This chapter covers both string constants (or literals, as they're called in the C standard) and string variables, which can change during the execution.
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:
C++ for Engineers and Scientists Second Edition Chapter 7 Completing the Basics.
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.
Strings, Pointers and Tools
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
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.
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.
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.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 13 Strings (Continued)
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
C Characters and Strings
INC 161 , CPE 100 Computer Programming
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
C Characters and Strings
Characters, C-Strings, and More About the string Class
CSE 303 Lecture 14 Strings in C
Chapter 8 - Characters and Strings
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.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C Characters and Strings
Presentation transcript:

Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training

Training C/C++EcoSoftware 2 String o Introduction String. o String variable. o Reading and writing string o Accessing character in a string o Using the string library o String idioms o Arrays of strings. o Analyzing and Transforming Strings o Converting Strings to Numerical Values o Working with Wide Character Strings

Training C/C++EcoSoftware 3 Introduction String o Introduction string (literal string)  A string constant is a sequence of characters.  Or symbols between a pair of double-quote characters. Example : printf("This is a string."); o How string literal are stored.  String literal as a character arrays.  Example : “This is a string”

Training C/C++EcoSoftware 4 String variable o Initializing a string variable.  String variable can be initialized at the same time. char date1[8] = “June 14”; char date1[0] = {‘J’,’u’,’n’,’e’,’ ‘,’1’,’4’,’\0’}; o Character array versus character pointer.  Character array : char date[] = “June 14”;  Character pointer: char *date = “June 14”; June14\0

Training C/C++EcoSoftware 5 Reading and writing string o Writing string with printf and puts.  The %s conversion specification allow printf to write string.  Example : char str[] = “Are you having fun yet ?”; printf(“%s\n”,str);  The C library provides puts function. puts(str);

Training C/C++EcoSoftware 6 Reading and writing string o Reading strings using scanf and gets.  scanf to read a string into array chars.  It skips white space.  Always store null character at the and of the string.  Syntax : scanf(“%s”,str); o The C library provides gets function.  The gets() function is the simplest method of accepting a string through standard input.  Input characters are accepted till the Enter key is pressed.  The gets() function replaces the terminating ‘\n’ new line character with the ‘\0’ character.  Syntax : gets(str);

Training C/C++EcoSoftware 7 Reading and writing string o Reading strings character by character.  Using a loop and getchar function to read a character and the store character in array.

Training C/C++EcoSoftware 8 Accessing character in a string o Using loop to accessing character in a string.  Example. Using array indexUsing pointer

Training C/C++EcoSoftware 9 Using C String library o The C library provides a rich set of function for performing operation on strings. Function nameTest for  strcpy(str1, str2);  strlen(str1);  strcat(str1, str2);  strcmp(str1, str2);  String copy function  Length of string  Joining strings function  Comparing string function

Training C/C++EcoSoftware 10 String copy function o The strcpy (string copy function).  Copies the value in one string onto another.  The value of str2 is copied onto str1  The return str1.  Syntax : strcpy(str1, str2); char *strcpy(char *str1,const char *str2);  Exampe : char *str2, *str1 = NULL; strcpy(str2, “abcde”); // str2 has content “abcde”. strcpy(str1,str2); // str1 has content “abcde”

Training C/C++EcoSoftware 11 String length function o Using strlen() to caculate length of a string.  Syntax :size_t strlen(const char *str1);  Example : int len = 0; len = strlen(“abc”); // len is now 3; len = strlen(“”); // len is now 0;

Training C/C++EcoSoftware 12 Joining Strings Using a Library Function o Joins two string values into one. o Syntax :  char *strcat(char *str1,const char *str2); o Example :  strcpy(str1,”abc”); // str1 has content “abc”.  strcat(str1,”def”); // now str1 has content “abcdef”

Training C/C++EcoSoftware 13 Comparing string function o Compares two strings and returns an integer value based on the results of the comparison. o The function returns a value:  Less than zero if str1<str2  Zero if str1 is same as str2  Greater than zero if str1>str2 o Syntax :  int strcmp(char *str1, const char *str2); o Example :  char str1[] = "The quick brown fox";  char str2[] = "The quick black fox";  int valuecmp = strcmp(str1,str2);

Training C/C++EcoSoftware 14 String idioms o Idioms by using theme to write strlen and strcat functions.  Search for ending of a string (strlen).

Training C/C++EcoSoftware 15 String idioms o Copy a string (strcat function).

Training C/C++EcoSoftware 16 Arrays of strings o Store the strings in the two-dimensional arrays. o Example :  char planets[][8]={“Mercury”,”Venus”}; o Image of strings in the two-dimensional arrays Mercury\0 Venus

Training C/C++EcoSoftware 17 Arrays of strings o Using pointer:  Example :  char *planets[]={“Mercury”,”Venus”}; o Images: planets 0 1 Mercury\0 Venus

Training C/C++EcoSoftware 18 Analyzing and Transforming Strings o The C library ctype.h have many Character Classification Functions. Function nameTest for  islower()  isupper()  isalpha()  isalnum()  iscntrl()  isprint()  isgraph()  isdigit()  isxdigit()  isblank()  isspace()  ispunct()  Lowercase letter  Uppercase letter  Uppercase or lowercase letter  Uppercase or lowercase letter or a digit  Control character  Any printing character including space  Any printing character except space  Decimal digit ('0' to '9')  Hexadecimal digit ('0' to '9', 'A' to 'F', 'a' to 'f')  Standard blank characters (space, '\t')  Whitespace character (space, '\n', '\t', '\v', '\r', '\f')  Printing character for which isspace() and isalnum() return false

Training C/C++EcoSoftware 19 Analyzing and Transforming Strings o Example :

Training C/C++EcoSoftware 20 Converting Strings to Numerical Values o The header file declares functions that you can use to convert a string to a numerical value. FunctionsReturns atof()A value of type double that is produced from the string argument atoi()A value of type int that is produced from the string argument. atol()A value of type long that is produced from the string argument. atoll()A value of type long long that is produced from the string argument

Training C/C++EcoSoftware 21 Converting Strings to Numerical Values o Examples:

Training C/C++EcoSoftware 22 Working with Wide Character Strings o Using the header file o Store a wide character string in an array of elements of type wchar_t. o Wide character string constant just needs the L modifier in front of it. o Exampe :  wchar_t proverb[] = L"A nod is as good as a wink to a blind horse."; o Display value.  printf("The proverb is:\n%S", proverb);

Training C/C++EcoSoftware 23 Operations on Wide Character Strings o Using the header file declares a range of functions for operating on wide character strings. o Functions That Operate on Wide Character Strings in. FunctionDescription wcslen(const wchar_t* ws)Returns a value of type size_t that is the length of the wide character string ws that you pass as the argument. The length excludes the termination L'\0' character. wcscpy(wchar_t* destination, const wchar_t source) Copies the wide character string source to the wide character string destination. The function returns source. wcscat(whar_t* ws1, whar_t* ws2) Appends a copy of ws2 to ws1. The first character of Ws2 overwrites the terminating null at the end of ws1. The function returns ws1.

Training C/C++EcoSoftware 24 Operations on Wide Character Strings FunctionDescription wcsncpy(wchar_t* destination, const wchar_t source, size_t n) Copies n characters from the wide character string source to the wide character string destination. wcsncmp(const wchar_t* ws1, const wchar_t* ws2) Compares the wide character string pointed to by ws1 with the wide character string pointed to by ws2 and returns a value of type int wcscmp(const wchar_t* ws1, const wchar_t* ws2, size_t n) Compares up to n characters from the wide character string pointed to by ws1 with the wide character string pointed to by ws2. wcsstr(const wchar_t* ws1, const wchar_t* ws2) Returns a pointer to the first occurrence of the wide character string ws2 in the wide character string ws1.

Training C/C++EcoSoftware 25 Testing and Converting Wide Characters o The header also declares functions to test for specific subsets of wide characters Function nameTest for  iswlower()  iswupper()  iswalpha()  iswalnum()  iswcntrl()  iswprint()  iswgraph()  iswdigit()  iswxdigit()  iswblank()  iswspace()  iswpunct()  Lowercase letter  Uppercase letter  Uppercase or lowercase letter  Uppercase or lowercase letter or a digit  Control character  Any printing character including space  Any printing character except space  Decimal digit (L'0' to L'9')  Hexadecimal digit (L'0' to L'9', L'A' to L'F', L'a' to L'f')  Standard blank characters (space, L'\t')  Whitespace character (space, L'\n', L'\t', L'\v', L'\r', L'\f')  Printing character for which isspace() and isalnum() return false

Training C/C++EcoSoftware 26 Example Converting Wide Characters

Training C/C++EcoSoftware 27 Thank You End