Strings in c++

SKAhsan 408 views 11 slides Apr 18, 2017
Slide 1
Slide 1 of 11
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11

About This Presentation

Strings In C++


Slide Content

STRINGS in C++ Sunawar Khan MCS, MS(CS)

Strings Strings are a fundamental concept, but they are not a built-in data type in C/C++. String is collection of characters. C-Strings C-style string: character array terminated by first null character. Wide string: wide character array terminated by first null character. C++ - Strings Several Classes Standard Template Class: std :: basic_string , std ::string, std :: wstring No inter-operability between C and C++ style strings.

String example C-string Array of chars that is null terminated (‘ \0 ’). C++ - string Object whose string type is defined in the <string> file has a large repertoire of functions (e.g. length, replace, etc.) char cs [ ] = “Napoleon”; // C-string string s = “Napoleon”; // C++ - string cout << s << “ has “ << s. length () << “ characters.\n”; s. replace (5, 2,”ia”); //changes s to “ Napolian

Strings C-style strings consist of a contiguous sequence of characters terminated by and including the first null character. A pointer to a string points to its initial character. The length of a string is the number of bytes preceding the null character The value of a string is the sequence of the values of the contained characters, in order. h e l l o \0 length

C++ Strings Formatted Input: Stream extraction operator cin >> stringObject ; the extraction operator >> formats the data that it receives through its input stream; it skips over whitespace Unformatted Input: getline function for a string getline ( cin , s) does not skip over whitespace delimited by newline reads an entire line of characters into s string s = “AB C DEFG”; getline ( cin , s); //reads entire line of characters into s char c = s[2]; //assigns ‘C’ to c S[4] = ‘*’; //changes s to “ABCD * FG”

strings

7 Functions

Program Code(ACSII) // This program demonstrates some of the character testing // functions. #include < iostream.h > #include < ctype.h > void main(void) { char input; cout << "Enter any character: "; cin.get (input); cout << "The character you entered is: " << input << endl ; cout << "Its ASCII code is: " << int (input) << endl ; if ( isalpha (input)) cout << "That's an alphabetic character.\n"; if ( isdigit (input)) cout << "That's a numeric digit.\n"; if ( islower (input)) cout << "The letter you entered is lowercase.\n"; if ( isupper (input)) cout << "The letter you entered is uppercase.\n"; if ( isspace (input)) cout << "That's a whitespace character.\n"; } Enter any character: A The character you entered is: A Its ASCII code is: 65 That's an alphabetic character. The letter you entered is uppercase. OUTPUT Enter any character: 7 [Enter] The character you entered is: 7 Its ASCII code is: 55 That's a numeric digit. OUTPUT

9 Character Case Conversion The C++ library offers functions for converting a character to upper or lower case. Be sure to include ctype.h header file

10 Sample Code // This program calculates the area of a circle. It asks the // user if he or she wishes to continue. A loop that // demonstrates the toupper function repeats until the user // enters 'y', 'Y', 'n', or 'N'. #include < iostream.h > #include < ctype.h > void main(void) { const float pi = 3.14159; float radius; char go; cout << "This program calculates the area of a circle.\n"; cout.precision (2); cout.setf ( ios ::fixed); do { cout << "Enter the circle's radius: "; cin >> radius; cout << "The area is " << (pi * radius * radius); cout << endl ; do { cout << "Calculate another? (Y or N) "; cin >> go; } while ( toupper (go) != 'Y' && toupper (go) != 'N'); } while ( toupper (go) == 'Y'); } Enter the circle's radius: 10 The area is 314.16 Calculate another? (Y or N) b Calculate another? (Y or N) y Enter the circle's radius: 1 The area is 3.14 Calculate another? (Y or N) n OUTPUT

11 String Function
Tags