Chapter 3 (Strings)
Chapter 3 (Strings)
CHAPTER 3
TOPIC: Strings
Kukutla Alekhya
Objectives
#include <iostream>
using namespace std;
int main( ) { Output:
string s1 = "Hello"; Hello
char ch[] = { 'C', '+', '+'}; C++
string s2 = string(ch);
cout<<s1<<endl;
cout<<s2<<endl;
}
String Compare Example
#include <iostream>
#include <iostream>
#include <cstring> Output:
using namespace std; What is my favourite fruit? apple
int main () What is my favourite fruit? banana
{ What is my favourite fruit? mango
char a[] = "mango"; Answer is correct!!
char b[50];
do {
cout<<"What is my favourite fruit? ";
cin>>b;
} while (strcmp (a,b) != 0);
cout<<"Answer is correct!!"<<endl;
return 0;
}
String Concat Example
#include <cstring>
using namespace std;
int main()
{
char a[25], b[25];
cout << "Enter the first string: ";
cin>>a; Output:
cout << "Enter the second string: "; Enter the key string: Welcome to
cin>>b; Enter the buffer string: C++ Programming.
strcat(a, b); Key = Welcome to C++ Programming.
cout << "a = " << a << endl; Buffer = C++ Programming.
cout << "b = " << b<<endl;
return 0;
}
String Copy Example
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char ary[] = "Welcome to C++ Programming";
cout << "Length of String = " << strlen(ary)<<endl;
return 0;
}
Length of String = 26
Learning outcomes