Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

#include <iostream>

#include <string>
using namespace std;

int main(){

// ##1

// char y[] = {"Hello Bitches"};


// char *p;
// p = y;
// cout<<p<<endl;
// cout<<*p<<endl;
// cout<<&y<<endl;
// cout<<y[1]<<endl;
// cout<<&y<<endl;
// cout<<y<<endl;

// ##2

// int x[5] = {91, 64, 82, 12, 21};


// int *l;
// l = x;
// cout<<l<<endl;
// cout<<*l<<endl;
// cout<<&x<<endl;
// cout<<x<<endl;

// char s[] = {'H', 'E', 'L', 'L', 'O', '\0'};


// cout<<s<<endl;

// char *ptr = "Hello";


// cout<<ptr;

// ##3

// string str;
// cout<<"Enter: ";
// cin>>str;
// cout<<str<<endl;

// cin>>str;
// cout<<str<<endl;

// cin>>str;
// cout<<str<<endl;

1
// ##4

// s.length(), s.size(), s.capacity(), s.clear(),


s.max_size(), s.resize(), s.empty()

// string str = "This is the string";


// cout<<str.length()<<endl; //calling the function
on the object str made out of the class string
// cout<<str.size()<<endl; //size and length
functions are the same
// cout<<str.capacity()<<endl; //gives the capacity
of the array in which the string is stored in
// str.resize(50); //resizes the capacity
of that array as per the user's wish
// cout<<str.capacity()<<endl; //capacity of the
array changed
// str.clear(); //clears string
// cout<<str.length()<<endl;
// cout<<str.max_size(); // max size of the
string allowed
// //str.empty(); //returns true if
string is empty (len = 0)

// ##5

// s.append(), s.insert(), s.replace(), s.erase(),


s.push_back(), s.swap()
// string str = "Dani Daniels has the best ass.";
// string str2 = "Hello World";
// string str3 = "How you?";
// string str4 = "Programming";
// string str5 = "AAAAA";
// string str6 = "BBBBB";
// cout<<str.capacity()<<endl;
// str.append(" Period."); //appends to the string
// cout<<str.capacity()<<endl; // capacity might or might
not change

// str2.insert(3, "xdxd"); // inserting "xdxd" at the


index 3 and shifting the rest of the characters of string
// cout<<str2<<endl;

// str3.insert(3, " area", 4); //inserting " area" at the


index 3 and only first 4 characters of " area" are used while
inserting
// cout<<str3<<endl;

// str4.replace(2, 4, "LOL"); //replacing 4 characters

2
from index 2 from the original string with "LOL"
// cout<<str4<<endl;

// //s.erase() erases the string


// str4.push_back('z'); //adding an element at the end
of the string
// cout<<str4<<endl;
// str3.pop_back(); //removing an element from the
end of the string
// cout<<str3<<endl;

// str5.swap(str6); //swapping two strings


// cout<<str5<<endl;
// cout<<str6<<endl;

// ##6

// s.copy(), s.find(), s.rfind(), s.find_first_of(),


s.find_last_of(), s.substr(), s.compare()

// string s = "Welcome Home Guys";


// string z = "Hello World";
// char str[50];
// s.copy(str, s.length()); //instead of the whole length
we can also copy a limited number of characters using s.copy(str,
3)
// str[17] = '\0'; //null character was not
getting copied
// cout<<str<<endl;
// cout<<s<<endl;

// cout<<s.find("come")<<endl; //output --> 3


// cout<<s.find("Guys")<<endl; //output --> 13
// cout<<s.find("bitches")<<endl; //output --> any arbitrary
number out of the range of string
// cout<<s.rfind("y")<<endl; //gives the index of 'y'
starting searching it from the right side
// cout<<s.find_first_of('l')<<endl; //outputs --> 2
// cout<<z.find_last_of('l')<<endl; //output --> 9
// cout<<z.find_first_of('l', 4)<<endl; //starts searching
for the first l from the index 4
// cout<<z.find_first_of("le")<<endl; //output --> 1
// cout<<z.find_first_of("yso")<<endl; //gives whichever
character appears the first, output --> 4
// cout<<z.substr(2)<<endl; //substring starting from
index 2
// cout<<z.substr(2,3)<<endl; //substring starting from
index 2 and goes for 3 characters
// cout<<z.compare(s)<<endl; //compares as per dictionary

3
//##7

// concatenation can be done directly by adding two strings


// string q = "Hello";
// string w = "Bitches";
// string e = q + w;
// cout<<e<<endl;

// string str_="today";
// string::iterator it;
// for(it = str_.begin(); it != str_.end(); it++)
// { // cout<<*it //dereferencing
// *it = *it - 32;
// }
// cout<<str_;

// string str__="today";
// string::reverse_iterator it;
// for(it = str__.rbegin(); it != str__.rend(); it++)
// {
// cout<<*it; //dereferencing
// }

//
// ###8
// string str;
// cout<<"Enter a string: ";
// getline(cin, str);
// int spaces = 0;
// int vowels = 0;
// int consonants = 0;
// int s = 0;
// for(int i = 0; i < str.length(); i++)
// {
// if (str[i] == ' ' && str[i+1] != ' ')
// {
// spaces += 1;
// }
// else if (str[i] == 'a' or (str[i] == 'e') or (str[i]
== 'i') or (str[i] == 'o') or (str[i] == 'u') or
// (str[i] == 'A') or (str[i] == 'E') or
(str[i] == 'I') or (str[i] == 'O') or (str[i] == 'U') )
// {
// vowels += 1;
// }
// else if (str[i] == ' ')
// s += 1;

4
// else
// {
// consonants += 1;
// }
// }
// cout<<vowels<<endl;
// cout<<consonants<<endl;
// cout<<spaces + 1<<endl;

// // ##
Palindrome ##
// string str;
// cout<<"Enter a string: ";
// getline(cin, str);
// string str2;
// for(int i = str.length() - 1; i >= 0; i--)
// str2 += str[i];

// if (str2 == str)
// {
// cout<<"Palindrome";
// }
// else
// {
// cout<<"Not a Palindrome";
// }

return 0;
}

You might also like