Download
strings n.
Skip this Video
Loading SlideShow in 5 Seconds..
Strings PowerPoint Presentation

Strings

168 Views Download Presentation
Download Presentation

Strings

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Strings Skill Area 313 Part C Materials Prepared by DhimasRuswanto, BMm

  2. Lecture Overview • Char • Strings

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

  4. Char • A string containing a single character takes up 2 bytes of storage.

  5. Char

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

  7. Char

  8. Char char message1[12] = “Hello world”; cout << message1 << endl; char message2[12]; cin >> message2; //type “Hello” as input

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

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

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

  12. Strings #include <string> //string header file stringmystr; //declare string getline (cin, mystr); //getline for string

  13. 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; }

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

  15. 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; }

  16. 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);

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

  18. --- continue to next slide ---