2 BytesC++ course_2014_c8_ strings
description
Transcript of 2 BytesC++ course_2014_c8_ strings
Kinan keshkeh
IT Engineering-Damascus University
3rd year
Summer course- 2014
2 bytes team
Strings !
Strings
1. C- String (Array of chars )
2. Objects from String Classs
C-Strings !
C-Strings
• It’s an Array of chars ends with ‘ \0 ’ (NULL character)
• Syntax :
char Array_Name[Maximum_C-string_Size + 1];
C-Strings
• char s[10+=“Hi Mom!”
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";
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 !!!
C-Strings
• char arr[5+=,‘k’,’i’,’n’,’a’,’n’-;
• char arr[10+=,‘k’,’i’,’n’,’a’,’n’-;
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 !
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
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
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
C-Strings
C-Strings
C-Strings
C-Strings
C-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 !!
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!!
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
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.
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
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);
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.
Character Manipulation Tools
Character Manipulation Tools
Character Manipulation Tools
Character Manipulation Tools
String !
String
• It’s a library . • Like any type . • You can use ‘+’, ‘=’, ‘==’ ,’<=’ .
String
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))
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; }
String
• If we have :
char aCString[] = "This is my C-string."; string stringVariable;
strcpy(aCString, stringVariable);
aCString = stringVariable;
aCString = stringVariable.c_str( );
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 !!
String
String
Code discussion 4
Code discussion 4
Code discussion 4
Code discussion 4
Code discussion 4
That’s for today
Have a good day !
Bye Bye !
2 bytes team
Group : group link
Mobile phone- Kinan : 0994385748
Facebook account : kinan’s account
2 bytes team