This document contains notes from a summer course on C strings and C++ strings. It discusses the basics of C strings as arrays of characters terminated by a null character. It also covers initializing and manipulating C strings, comparing strings, and input/output. For C++ strings, it discusses the string class and methods for accessing and manipulating characters within a string. The document concludes with examples of converting between C strings and C++ strings.
19. C-Strings
•char aString[10]; aString = "Hello";
An Error , you cant write that !!!
•char aString[10]; strcpy(aString ," Hello");
But with strcpy u can !!
20. 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!!
21. 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
22. 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.
24. 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);
25. 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.
33. 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))
34. 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; }
35. String
•If we have :
char aCString[] = "This is my C-string.";
string stringVariable;
strcpy(aCString, stringVariable);
aCString = stringVariable;
aCString = stringVariable.c_str( );
36. 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 !!