Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming C/C++ on Eclipe Trình bày : Ths HungNM C/C++ Training."— Presentation transcript:

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

2 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

3 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”

4 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

5 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);

6 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);

7 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.

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

9 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

10 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”

11 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;

12 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”

13 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);

14 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).

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

16 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

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

18 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

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

20 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

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

22 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);

23 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.

24 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.

25 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

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

27 Training C/C++EcoSoftware 27 Thank You End


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

Similar presentations


Ads by Google