180 likes | 378 Views
Strings. Skill Area 313 Part C. Materials Prepared by Dhimas Ruswanto , BMm. Lecture Overview. Char Strings. Char. A sequence of characters is often referred to as a character “string”. A string is stored in an array of type char ending with the null character “”. Char.
E N D
Strings Skill Area 313 Part C Materials Prepared by DhimasRuswanto, BMm
Lecture Overview • Char • Strings
Char • A sequence of characters is often referred to as a character “string”. • A string is stored in an array of type charending with the null character “\0”.
Char • A string containing a single character takes up 2 bytes of storage.
Char • A string constant is a sequence of characters enclosed in double quotes. • E.g. char s1[2]=“a”; //takes 2 bytes of storage • On the other hand, the character, in single quotes: • E.g. char s2=‘a’; //takes only a byte of storage
Char char message1[12] = “Hello world”; cout << message1 << endl; char message2[12]; cin >> message2; //type “Hello” as input
getline • The function getline can be used to read an entire line of input into a string variable. • The getline function has three parameters: • The first specifies the area into which the string is to be read. • The second specifies the maximum number of characters, including the string delimiter. • The third specifies an optional terminating character. If not included, getline stops at “\n”.
getline charA_string[80]; //80 is the size of A_string cout << “Enter some words in a string: \n”; cin.getline (A_string, 80); cout << A_string << “\nEnd of Output\n”; Output: Enter some words in a string: This is a test. This is a test. End of Output
getline charlastName[30], firstName[30]; cout << “Enter a name <last,first>: \n”; cin.getline (lastName, sizeof(lastName)); cin.getline (firstName, sizeof(firstName)); cout << “Here is the name you typed: \n\t|” << firstName << “ ” <<lastName << “|\n”; Output: Enter a name <last, first>: Chan Anson Here is the name you typed: |Anson Chan|
Strings #include <string> //string header file stringmystr; //declare string getline (cin, mystr); //getline for string
Example (Strings) #include <iostream> #include <string> usingnamespacestd; intmain () { stringmystr; cout << "What's your name? "; getline (cin, mystr); cout << "Hello " << mystr << ".\n"; cout << "What is your favorite team? "; getline (cin, mystr); cout << "I like " << mystr << " too!\n"; return 0; }
stringstream • The standard header file <sstream> defines a class called stringstream that allows a string-based object to be treated as a stream. This way we can perform extraction or insertion operations from/to strings, which is especially useful to convert strings to numerical values and vice versa. • For example, if we want to extract an integer from a string we can write: string mystr ("1204"); intmyint; stringstream(mystr) >> myint;
Example (stringstream) #include <iostream> #include <string> #include <sstream> usingnamespacestd; intmain () { stringmystr; float price=0; intquantity=0; cout << "Enter price: "; getline (cin,mystr); stringstream(mystr) >> price; cout << "Enter quantity: "; getline (cin,mystr); stringstream(mystr) >> quantity; cout << "Total price: " << price*quantity <<endl; return 0; }
strcpy char name1[16], name2 [16]; strcpy(name2, name1) //copies string name2 into string name1 Name1[16]=“Chan Tai Man” Name2[16]=“9999999999999999” strcpy(name2, name1);
Examples Exchange the value of 2 numbers; inta,b,temp; cout<< “Enter a: ”; cin >> a; cout << “Enter b: “; cin >> b; temp=a; a=b; b=temp; cout << a <<endl; cout << b << endl;