27
String data type Cs 240

String data type

  • Upload
    sibyl

  • View
    56

  • Download
    0

Embed Size (px)

DESCRIPTION

String data type. Cs 240. Char & string. char : holds a single character string : holds a sequence of characters Both can be used in assignment statements Both can be displayed with cout and

Citation preview

Page 1: String  data type

String data type

Cs 240

Page 2: String  data type

Char & string

• char: holds a single character

• string: holds a sequence of characters

• Both can be used in assignment statements

• Both can be displayed with cout and <<

Page 3: String  data type

Character Input

Reading in a character

char ch;cin >> ch; // Reads in any non-blank char

Page 4: String  data type

What is a String?

• Generally speaking, a string is a sequence of characters

• Examples:• “hello” • “high school”• “H2O”• “4620000”

Page 5: String  data type

5

String type

• To use the data type string, the program must include the header file <string>

#include <string>

Page 6: String  data type

Declaration of strings• string name; //declare and initialize to empty string• string name = “Sara”; //declare and initialize to Sara• string name(“Sara”); //declare and initialize to Sara• string name = “Sara Ali”; //declare and initialize to Sara Ali• string name(“Sara Ali”); //declare and initialize to Sara AliNOTE:Empty string “”

Page 7: String  data type

Declaration of stringsstring name= “Sara Ahmad";

declares name to be a string variable and also initializes name to “Sara Ahmad"

• The first character in name, ‘S', is in position 0, the second character, ‘a', is in position 1, and so on

• The variable name is capable of storing any size string

Page 8: String  data type

Declaration of strings

• string str = ‘m’;• string str2 = 22;• BOTH ARE NOT CORRECT, and result in syntax

error!

Page 9: String  data type

String Input

Reading in a string objectstring str; cin >> str;

getline(cin, str);

• getline reads till a newline ‘\n’ is encountered.Read from the keyborad

Reads in a string with no blanks (reads one word)

Reads in a string that may contain blanks(Reads entire line)

Page 10: String  data type

String Input

cin >> name; Type in “Alice Wonderland” Result: name “Alice”

• Instead use

getline (cin, name); Result: name “Alice

Wonderland”

Page 11: String  data type

String input#include<iostream>#include<string>using namespace std;

int main(){

string name;cout<<"Enter you name : ";cin>>name;cout<<"Hello "<<name<<endl;return 0;

}

Page 12: String  data type

String input#include<iostream>#include<string>using namespace std;

int main(){

string name;cout<<"Enter you name and your family name : ";cin>>name;cout<<"Hello "<<name<<endl;return 0;

}

#include<iostream>#include<string>

using namespace std;

int main)({string name;

cout<<"Enter you name and your family name;" :

getline(cin,name);cout<<"Hello "<<name<<endl;return 0;

}

Page 13: String  data type

Word at a Time Input#include <iostream>

#include <string>using namespace std;

int main)({

string str1; while (true)

{ cout << "Enter a string;"

cin >> str1; cout << "You entered: " << str1;

cout << endl;} }

Page 14: String  data type

Word at a Time Input

Page 15: String  data type

Line at a Time Input#include <iostream>

#include <string>using namespace std;

int main)({

string str1; while (true)

{ cout << "Enter a string;"

getline(cin,str1); cout << "You entered: " << str1;

cout << endl;} }

Page 16: String  data type

Line at a Time Input

Page 17: String  data type

String Assignment

targetString = sourceString;OR

targetString.assign(sourceString);OR

targetString.assign(sourceString,start,numOfCharacters);

Page 18: String  data type

String Assignment

Page 19: String  data type

String Concatenation

• str1 = str2 + str3;• str1.append(“XXX”);• str1.append(str2);• str1 += str2;

Page 20: String  data type

String Concatenation

Page 21: String  data type

String SizestrVar.size();

Page 22: String  data type

Comparing strings

• Using Relational and equality operator:== != > < • str1.compare(str2);

return 0 if str1 and str2 are equalsreturn + number if str1 > str2return – number if str1 < str2

• str1.compare(pos1,n1,str2);• str1.compare(pos1,n1,str2,pos2,n2);

Page 23: String  data type

Comparing strings

Page 24: String  data type

substrings• str1.substr(startPos,length);

Page 25: String  data type

Swapping strings

• str1.swap(str2);

Page 26: String  data type

Finding substrings in a string

• str1.find(str2);attempts to find str2 in str1.if str2 is found, the subscript of the starting location of that string is returned.If str2 is not found, the value string::npos is returned.

• str1.rfind(str2);search backward (right-to-left) if str2 is found, the subscript of the starting location of that string is returned.If str2 is not found, the value string::npos is returned.

Page 27: String  data type

Finding substrings in a string