Download presentation
Presentation is loading. Please wait.
1
C++ Programming Basics
Arrays
2
Array Array – data structure with homogeneous data type
a set of data represented by a single variable name Element – individual data contained in an array Declaration of array – type name [ index # ] ; Index number (or subscript) is used to identify each element – index number begins with 0, not 1 char array – single quote ( ‘ ) is used Text string – double quote ( “ ) Initialization with text string entails \0 (end mark) at the end
3
char Grades[5]; // Grades with 5 elements
Array example char Grades[5]; // Grades with 5 elements Grades[0] = ‘A’ ; // first element Grades[1] = ‘B’ ; Grades[2] = ‘C’ ; Grades[3] = ‘D’ ; Grades[4] = ‘F’ ; // 5th element Array declaration with initialization char Grades[5] = {‘A’, ‘B’, ‘C’, ‘D’, ‘F’} ; char Grades[] = {‘A’, ‘B’, ‘C’, ‘D’, ‘F’} ; char Grades[5] = “ABCDF” ; // error char Grades[] = “ABCDF” ;
4
String Variable Declaration syntax
char string_name [size] ; Empty string variable with character size of [size] char string_name [size] = “Hello” ; String variable with initial value of “Hello” In this case, size can be omitted Text string can be assigned to string variable only when it is declared. Once declared, text string can not be assigned to the string variable with a normal assignment operator ( = ). 4
5
String Variable How to display a string variable
char strName[20] = “Smith” ; cout << strName << endl ; cout << strName[4] << endl; Even though string variable is a char array, it can be used as a normal variable, ignoring any array features it can be used like a char array, too 5
6
String Manipulation strcat(dest_variable, string) – append one string to another strchr(string, character) – find the first occurrence of a specified character in a string strcmp(string1, string2) – compare two string strcpy(dest_variable, string) – replace the contents of one string with the contents of another strcspn(string, char_set) – find the first occurrence in a string of a specified character within a specified character set strlen(string) – return the length of a string 6
7
strncat(dest_variable, source_char, source_max_size) – append characters to a string
strncmp(string1, string2, max_size) – compare first max_size characters within two strings strncpy(dest_variable, string, max_size) – copy first max_size characters from one string to another strpbrk(string, char_set) – find the first occurrence in a string of a specified character set in another string strrchr(string, char) – find the last occurrence in a string of a specified character strspn(string, char_set) – find the first occurrence of not-specified char in character set strstr(string, source_string) – find the first occurrence of source string in another specified string 7
8
String Pointer Variable
String pointer – use of a pointer that indirectly points to a series of characters String pointer is not a fixed size, so any size of text string can be assigned with an assignment operator Syntax char *strPointer ; strPointer = “Hello”; 8
9
String Object Variable
String object – string class is not a normal data type; it can hold any size of text string Requires <string> header String object is also a char array, so it can be used like an array Syntax string strObject ; strObject = “Hello” ; cout << strObject << endl; cout << strObject[4] << endl; 9
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.