PF Week 9
PF Week 9
STRINGS
Strings are used to represent text. They are set of characters that can also contain alphabets,
spaces, numbers and other symbols. C++ provides following two types of string representations:
• The C-style character string.
• The string class type introduced with Standard C++.
C-STRINGS
The C-String originated within the C language and continues to be supported within C++. This string
is actually a one-dimensional array of characters which is terminated by a null character '\0'. Thus a
null-terminated string contains the characters that comprise the string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To hold
the null character at the end of the array, the size of the character array containing the string is one
more than the number of characters in the word "Hello."
Declaration Syntax:
char str [Size];
Initialization:
char str [] = {'H','e','l','l','o','\0'};
OR
char str[] = "Hello";
#include <iostream>
using namespace std;
int main()
{
char str1[] = {'H','e','l','l','o','\0'};
char str2[] = "Welcome";
cout<<str1<<" "<<str2<<endl;
char str3[20]; //string variable str
cout << "Enter a string: ";
cin.get(str3, 20); //put string in str
cout << "You entered: " << str3 << endl;
return 0;
}
#include <iostream>
using namespace std;
const int MAX = 2000;
char str[MAX];
int main()
{
cout << "Enter a string:"<<endl;
cin.get(str, MAX, '$'); //terminate with $
cout << "You entered:"<<endl << str << endl;
return 0;
}
You can also use the += operator to append a string to the end of an existing string:
s3 += s2;
#include <iostream>
#include <string> //for string class
using namespace std;
int main()
{ //objects of string class
string full_name, nickname, address;
string greeting(“Hello, “);
cout << “Enter your full name: “;
getline(cin, full_name); //reads embedded blanks
cout << “Your full name is: “ << full_name << endl;
cout << “Enter your nickname: “;
cin >> nickname; //input to string object
greeting += nickname; //append name to greeting
cout << greeting << endl; //output: “Hello, Jim”
cout << “Enter your address on separate lines\n”;
cout << “Terminate with ‘$’\n”;
getline(cin, address, ‘$’); //reads multiple lines
cout << “Your address is: “ << address << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main(){
string s1 = "We all should respect everyone";
cout<<s1<<endl;
cout<<"Total Characters: "<<s1.length()<<endl;
int n;
n = s1.find("respect");
cout << "Found respect at index: " << n << endl;
n = s1.find_first_of("abcd");
cout << "First of spde at index: " << n << endl;
n = s1.find_first_not_of("aeiouAEIOU");
cout << "First consonant at: " << n << endl;
return 0;
}
#include <iostream>
#include <string>
using namespace std;
int main() {
string s1 = "His Name is Rizwan";
string s2 = s1.substr(12);
cout<<s1<<endl;
cout<<s1.substr(4,4)<<":"<<s2;
return 0;
}
Function doesn’t need the size of the first dimension as two dimensional array is an array of arrays.
It doesn’t need to know how many elements there are, but it does need to know how big each
element is. Following is the declaration of one dimensional array:
void somefunc( int elem[] );
void display(char[3][3]);
int main(){
char arr[3][3]={{' ',' ',' '},
{' ',' ',' '},
{' ',' ',' '}};
for (int i=0 ; i<3 ; i++)
for (int j=0 ; j<3 ; j++)
{
display(arr);
cout<<"Please Enter X or O";
cin>>arr[i][j];
system("cls");
}
}
void display(char a[3][3])
{
cout<<"\n\n\n -----------\n";
for (int i=0 ; i<3 ; i++){
for (int j=0 ; j<3 ; j++)
cout<<" | "<<a[i][j];
cout<<" |\n -----------\n\n";
}
}
EXERCISE 1:
Write a simple program that asks the user for a Car registration number and then verifies that by:
• Using a length function to make sure 6 characters were entered, use cout for displaying
error if user enters less or more letters.
• Check the first three character to make sure they are alphabetic. You can use
the isalpha(char) function for this.
• Check the last three character to make sure they are numeric. You can use
the isdigit(char) function for this.
• Include “ctype.h” for both isalpha() and isdigit() functions
Enter car registration number: GF121
Sorry Enter again
Enter car registration number: B121N1
Sorry Enter again
Enter car registration number: BAN123
Registration Number Verified
EXERCISE 2:
Write a program that reads a commercial website URL from user; you should expect that the URL
starts with ‘www.’ and ends with ‘.com’
Retrieve the name of the site and output it. For instance, if the user inputs www.yahoo.com, your
program should output yahoo.
Enter URL: www.youtube.com
Name of Site: youtube