2 BytesC++ course_2014_c8_ strings

45
Kinan keshkeh IT Engineering-Damascus University 3 rd year Summer course- 2014 2 bytes team

description

This is my C++ course / basic , intermediate and a little advance

Transcript of 2 BytesC++ course_2014_c8_ strings

Page 1: 2 BytesC++ course_2014_c8_ strings

Kinan keshkeh

IT Engineering-Damascus University

3rd year

Summer course- 2014

2 bytes team

Page 2: 2 BytesC++ course_2014_c8_ strings

Strings !

Page 3: 2 BytesC++ course_2014_c8_ strings

Strings

1. C- String (Array of chars )

2. Objects from String Classs

Page 4: 2 BytesC++ course_2014_c8_ strings

C-Strings !

Page 5: 2 BytesC++ course_2014_c8_ strings

C-Strings

• It’s an Array of chars ends with ‘ \0 ’ (NULL character)

• Syntax :

char Array_Name[Maximum_C-string_Size + 1];

Page 6: 2 BytesC++ course_2014_c8_ strings

C-Strings

• char s[10+=“Hi Mom!”

Page 7: 2 BytesC++ course_2014_c8_ strings

C-Strings

• It’s an Array of chars ends with ‘ \0 ’ (NULL character)

• Syntax :

char Array_Name[Maximum_C-string_Size + 1];

• initialize:

• char arr*+=“kinan”

• char arr[10+=“kinan”

• char aString[10]; aString = "Hello";

Page 8: 2 BytesC++ course_2014_c8_ strings

C-Strings

• It’s an Array of chars ends with ‘ \0 ’ (NULL character)

• Syntax :

char Array_Name[Maximum_C-string_Size + 1];

• initialize:

• char arr*+=“kinan”

• char arr[10+=“kinan”

• char aString[10]; aString = "Hello";

An Error , you cant write that !!!

Page 9: 2 BytesC++ course_2014_c8_ strings

C-Strings

• char arr[5+=,‘k’,’i’,’n’,’a’,’n’-;

• char arr[10+=,‘k’,’i’,’n’,’a’,’n’-;

Page 10: 2 BytesC++ course_2014_c8_ strings

C-Strings

• char arr[5+=,‘k’,’i’,’n’,’a’,’n’-;

• char arr[10+=,‘k’,’i’,’n’,’a’,’n’-;

An Error cause the ‘\0’ isn’t added

• char arr[6+=,‘k’,’i’,’n’,’a’,’n’-;

This is true !

Page 11: 2 BytesC++ course_2014_c8_ strings

C-Strings

1. #include <iostream>; 2. using namespace std; 3. int main(){ 4. char A[]="Pascal", B[20]="Ahmad" ; 5. cout <<"A contains '"<< A<<"'" 6. <<", its size :"<< sizeof (A) <<endl; 7. cout <<"B contains '"<< B<<"'" 8. <<", its size :"<< sizeof (B) <<endl; 9. }

A contains 'Pascal', its size :7 B contains 'Ahmad', its size :20

Page 12: 2 BytesC++ course_2014_c8_ strings

C-Strings

1. #include <iostream>; 2. using namespace std; 3. int main(){ 4. char A[]="Pascal", B[20]="Ahmad" ; 5. cout <<"A contains '"<< A<<"'" 6. <<", its size :"<< sizeof (A) <<endl; 7. cout <<"B contains '"<< B<<"'" 8. <<", its size :"<< sizeof (B) <<endl; 9. }

A contains 'Pascal', its size :7 B contains 'Ahmad', its size :20

Page 13: 2 BytesC++ course_2014_c8_ strings

C-Strings

#include <iostream>; using namespace std; int main(){ char ourString[20]="Blaise Pascal" ; int index = 0; while (ourString[index] != '\0') { if (ourString[index]==' ') ourString[index]='-'; index++; } cout << ourString; }

Blaise-Pascal

Page 14: 2 BytesC++ course_2014_c8_ strings

C-Strings

Page 15: 2 BytesC++ course_2014_c8_ strings

C-Strings

Page 16: 2 BytesC++ course_2014_c8_ strings

C-Strings

Page 17: 2 BytesC++ course_2014_c8_ strings

C-Strings

Page 18: 2 BytesC++ course_2014_c8_ strings

C-Strings

Page 19: 2 BytesC++ course_2014_c8_ strings

C-Strings

• char aString[10]; aString = "Hello";

An Error , you cant write that !!!

• char aString[10]; strcpy(aString ," Hello");

But with strcpy u can !!

Page 20: 2 BytesC++ course_2014_c8_ strings

C-Strings

#include <iostream> #include<cstring> using namespace std; int main(){ char ourString[14]="KINAN" ; strcpy(ourString,"keshkeh!!"); cout<<ourString<<endl; system("pause"); return 0 ;}

keshkeh!!

Page 21: 2 BytesC++ course_2014_c8_ strings

C-Strings

#include <iostream> #include<cstring> using namespace std; int main(){ char ourString[6]="KINAN" ; strcpy(ourString,"keshkehhhhhhhhhhhhhhhhhhhhhhhhhh!!"); cout<<ourString<<endl; system("pause"); return 0 ;} keshkehhhhhhhhhhhhhhhhhhhhhhhhhh!!

compile and run but with warring

Page 22: 2 BytesC++ course_2014_c8_ strings

C-Strings

#include <iostream> #include<cstring> using namespace std; int main(){ char ourString[20]="Blaise Pascal"; if (strcmp(ourString, "Blaise Pascal")) cout << "The strings are NOT the same."; else cout << "The strings are the same."; system("pause"); return 0 ;}

The strings are the same.

Page 23: 2 BytesC++ course_2014_c8_ strings

C-Strings

#include <iostream> #include<cstring> using namespace std; int main(){ char ourString[20]="Blaise Pascal"; cout<<strcmp(ourString, "Blaise Pascal")<<endl; system("pause"); return 0 ;}

0

Page 24: 2 BytesC++ course_2014_c8_ strings

C-Strings

The

Operation

Methods

Input /output

const int size=10; char arr[size]; cin>>arr;

const int max=10; char arr[max]; cin.getline( arr,max);

const int max=20; char arr[max]; cin.get( arr,max,’$’); /* ’$’ when u will end*/

cout<<arr;

cout<<arr; cout<<arr;

ends in Enter+doesn’t take spaces (ex”ki sks” take ”ki” ) so you can’t input more than

Takes the spaces ‘ ‘ ,but doesn’t take Enter if any (ex “ ki na n keshkeh” , take “ki na n“(till Eenter))

-Takse Enter + spaces (ex “ki n a n ke shkeh ” take all (till 20 chars)) - it can be: cin.get( char);

Page 25: 2 BytesC++ course_2014_c8_ strings

C-Strings

cout << "Enter a line of input and I will echo it:\n"; char symbol; do { cin.get(symbol); cout << symbol; - while (symbol != ’\n’); cout << "That’s all for this demonstration.\n";

Enter a line of input and I will echo it: Do Be Do 1 2 34 Do Be Do 1 2 34 That’s all for this demonstration.

Page 26: 2 BytesC++ course_2014_c8_ strings

Character Manipulation Tools

Page 27: 2 BytesC++ course_2014_c8_ strings

Character Manipulation Tools

Page 28: 2 BytesC++ course_2014_c8_ strings

Character Manipulation Tools

Page 29: 2 BytesC++ course_2014_c8_ strings

Character Manipulation Tools

Page 30: 2 BytesC++ course_2014_c8_ strings

String !

Page 31: 2 BytesC++ course_2014_c8_ strings

String

• It’s a library . • Like any type . • You can use ‘+’, ‘=’, ‘==’ ,’<=’ .

Page 32: 2 BytesC++ course_2014_c8_ strings

String

Page 33: 2 BytesC++ course_2014_c8_ strings

String

The

Operation

Methods

Input /output

String name; cin>>name;

String name; getline( cin , name);

String name; getline( cin , name,’$’); /* ’$’ when u will end*/

cout<<name;

cout<<name; cout<<name;

ends in Enter+doesn’t take spaces (ex”ki sks” take ”ki” ) so you can’t input more than

Takes the spaces ‘ ‘ ,but doesn’t take Enter if any (ex “ ki na n keshkeh” , take “ki na n“(till Eenter))

-Takse Enter + spaces (ex “ki n a n ke shkeh ” take all (till 20 chars))

Page 34: 2 BytesC++ course_2014_c8_ strings

String

• You can access the characters in a string object in the same way that you access array elements

#include<iostream> #include<string> using namespace std; int main( ) { string name="ali"; for (unsigned int i=0;i<name.length();i++) name[i]=toupper(name[i]); //or name.at(i)=toupper(name.at(i)); cout << name << endl; }

Page 35: 2 BytesC++ course_2014_c8_ strings

String

• If we have :

char aCString[] = "This is my C-string."; string stringVariable;

strcpy(aCString, stringVariable);

aCString = stringVariable;

aCString = stringVariable.c_str( );

Page 36: 2 BytesC++ course_2014_c8_ strings

String

• If we have :

char aCString[] = "This is my C-string."; string stringVariable;

strcpy(aCString, stringVariable);

aCString = stringVariable;

aCString = stringVariable.c_str( );

False !!

strcpy(stringVariable.c_str( ), aCString); True !!

Page 37: 2 BytesC++ course_2014_c8_ strings

String

Page 38: 2 BytesC++ course_2014_c8_ strings

String

Page 39: 2 BytesC++ course_2014_c8_ strings

Code discussion 4

Page 40: 2 BytesC++ course_2014_c8_ strings

Code discussion 4

Page 41: 2 BytesC++ course_2014_c8_ strings

Code discussion 4

Page 42: 2 BytesC++ course_2014_c8_ strings

Code discussion 4

Page 43: 2 BytesC++ course_2014_c8_ strings

Code discussion 4

Page 44: 2 BytesC++ course_2014_c8_ strings

That’s for today

Have a good day !

Bye Bye !

Page 45: 2 BytesC++ course_2014_c8_ strings

2 bytes team

Group : group link

Mobile phone- Kinan : 0994385748

Facebook account : kinan’s account

2 bytes team