Wednesday, August 12, 2020

Working with String in C++


Program Reference: Cpp_Programs/ Day_05/ Prog_01.cpp

#include<iostream>
#include<string>

using namespace std;

int main (void)
{
//DECLARING OBJECT UNDER THE CLASS string 
  string Str;
  char t;

  cout<<"\nWorking with String Class....";

  cout<<"\n\tSize if String Allocated(Initially): "<<Str.capacity();

//READ A STRING USING getline()
  cout<<"\n\n\tEnter a String: ";
  getline(cin, Str); //for string input
  cout<<"\t\tString Entered: "<<Str<<".";
  cout<<"\n\t\tLength of String: "<<Str.length();
  cout<<"\n\t\tSize of String Allocated(After Input): "<<Str.capacity();

//RESIZE
  Str.resize(20);
  cout<<"\n\n\tString Entered(After Resize): "<<Str<<".";
  cout<<"\n\tSize of String Allocated(After Resize): "<<Str.capacity();

//SHRINK
  cout<<"\n\n\tEnter New String: ";
  getline(cin, Str); //for string input
  cout<<"\t\tNew String Entered: "<<Str<<".";
  Str.shrink_to_fit();
  cout<<"\n\t\tString Entered(After Shrink): "<<Str<<".";
  cout<<"\n\t\tSize of String Allocated(After Shrink): "<<Str.capacity();

//APPEND A CHARACTER AT THE END
  cout<<"\n\n\tEnter a Character to Append: ";
  cin>>t;
  Str.push_back(t);  //to append at the end of the Str
  cout<<"\tString is(After Append): "<<Str<<".";
 
//DELETE A CHARACTER FROM THE END
  Str.pop_back();  //to append at the end of the Str
  cout<<"\n\n\tString is(After Delete Last Character): "<<Str<<".";

  cout<<"\nEnd of the Program....\n\n";

}


Output

Working with String Class....
    Size if String Allocated(Initially): 0

    Enter a String: Test
        String Entered: Test.
        Length of String: 4
        Size of String Allocated(After Input): 4

    String Entered(After Resize): Test.
    Size of String Allocated(After Resize): 20

    Enter New String: Testing
        New String Entered: Testing.
        String Entered(After Shrink): Testing.
        Size of String Allocated(After Shrink): 7

    Enter a Character to Append: s
    String is(After Append): Testings.

    String is(After Delete Last Character): Testing.
End of the Program....



Program Reference: Cpp_Programs/ Day_05/ Prog_02.cpp

#include<iostream>
#include<string>
#include<iterator>

using namespace std;

int main (void)
{
  cout<<"\nWorking with String Class....";

//DECLARATION WITH INITIALIZATION
  string S1("Initial String....");
  cout<<"\n\tInitial String is: "<<S1;

  string S2(S1);
  cout<<"\n\tSecond String is: "<<S2;

  string S3(10, '*');
  cout<<"\n\tThird String is: "<<S3;

  string S4(S1, 1, 4);
  cout<<"\n\tFourth String is: "<<S4;

  string S5(S1.begin(), S1.begin()+S1.length());
  cout<<"\n\tFifth String is: "<<S5;

  cout<<"\nEnd of the Program....\n\n";

}


Output

Working with String Class....
    Initial String is: Initial String....
    Second String is: Initial String....
    Third String is: **********
    Fourth String is: niti
    Fifth String is: Initial String....
End of the Program....



Program Reference: Cpp_Programs/ Day_05/ Prog_03.cpp

#include<iostream>
#include<string>
#include<iterator>

using namespace std;

int main (void)
{
  string Str1, Str2, Str3;
  cout<<"\nWorking with String Class....";


  cout<<"\n\tEnter a String: "; getline(cin, Str1);
  cout<<"\tEnter another String: "; getline(cin, Str2);

  cout<<"\n\tAppending String....";
  Str3.append(Str1);
  Str3.append(" ");
  Str3.append(Str2);
  cout<<"\n\tResult: "<<Str3;

  Str3.clear();
  cout<<"\n\tResult after Clear: "<<Str3;

  string Str4(Str1);
  cout<<"\n\n\tSub-String("<<Str1<<", "<<"1, 5): "<<Str4.substr(1, 5);

  string Str5(Str1);
  cout<<"\n\n\tErasing("<<Str1<<", "<<"1, 2): "<<Str5.erase(1, 2);

  string Str6(Str1);
  cout<<"\n\n\tReplace("<<Str1<<", "<<"1, 2, \"XXXXXXX\"): "<<Str6.replace(1, 2, "XXXXXXX");

  cout<<"\nEnd of the Program....\n\n";

}


Output

Working with String Class....
    Enter a String: Test String
    Enter another String: Data

    Appending String....
    Result: Test String Data
    Result after Clear:

    Sub-String(Test String, 1, 5): est S

    Erasing(Test String, 1, 2): Tt String

    Replace(Test String, 1, 2, "XXXXXXX"): TXXXXXXXt String
End of the Program....



Program Reference: Cpp_Programs/ Day_05/ Prog_04.cpp

#include<iostream>
#include<string>
#include<iterator>

using namespace std;

int main (void)
{
//DECLARING OBJECT UNDER THE CLASS string 
  string Str;
  string::iterator Start, End, iPtr;

  cout<<"\nWorking with String Class....";

//READ A STRING USING getline()
  cout<<"\n\tEnter a String: ";
  getline(cin, Str); //for string input

//DISPLAYING USING ITERATOR
  cout<<"\tString Entered: ";
  Start= Str.begin();
  End= Str.end();
  for (iPtr=Start; iPtr!=End; iPtr++) cout<<*iPtr;

  cout<<"\nEnd of the Program....\n\n";

}


Output

Working with String Class....
    Enter a String: Test Data
    String Entered: Test Data
End of the Program....



Program Reference: Cpp_Programs/ Day_05/ Prog_05.cpp

#include<iostream>
#include<string>
#include<iterator>

using namespace std;

int main (void)
{
//DECLARING OBJECT UNDER THE CLASS string 
  string Str;
  string::reverse_iterator Start, End, iPtr;

  cout<<"\nWorking with String Class....";

//READ A STRING USING getline()
  cout<<"\n\tEnter a String: ";
  getline(cin, Str); //for string input

//DISPLAYING USING ITERATOR
  cout<<"\tString(in Reverse) Entered: ";
  Start= Str.rbegin();
  End= Str.rend();
  for (iPtr=Start; iPtr!=End; iPtr++) cout<<*iPtr;

  cout<<"\nEnd of the Program....\n\n";

}


Output

Working with String Class....
    Enter a String: Test Data
    String(in Reverse) Entered: ataD tseT
End of the Program....



Program Reference: Cpp_Programs/ Day_05/ Prog_06.cpp

#include<iostream>
#include<string>
#include<iterator>

using namespace std;
//using namespace iterator;

int main (void)
{
//DECLARING OBJECT UNDER THE CLASS string 
  string Str;

  cout<<"\nWorking with String Class....";

//READ A STRING USING getline()
  cout<<"\n\tEnter a String: ";
  getline(cin, Str); //for string input

//DISPLAYING USING INDEX
  cout<<"\tString Entered: ";
  for (int i=0; i<Str.length(); i++) cout<<Str.at(i);

  cout<<"\nEnd of the Program....\n\n";

}


Output

Working with String Class....
    Enter a String: Test Data
    String Entered: Test Data
End of the Program....



Program Reference: Cpp_Programs/ Day_05/ Prog_07.cpp

#include<iostream>
#include<string>
#include<iterator>

using namespace std;
//using namespace iterator;

int main (void)
{
//DECLARING OBJECT UNDER THE CLASS string 
  string StrS, StrD;
  int i;

  cout<<"\nWorking with String Class....";

//READ A STRING USING getline()
  cout<<"\n\tEnter a String: ";
  getline(cin, StrS); //for string input

//COPY BY ACCESSING EACH CHARACTER INDIVIDUALLY
  StrD.resize(StrS.length());
  for (i=0; i<StrS.length(); i++) StrD.at(i)=StrS.at(i);
  cout<<"\n\tString(Source): "<<StrS<<"\n\tString(Destination): "<<StrD;

  cout<<"\nEnd of the Program....\n\n";

}


Output

Working with String Class....
    Enter a String: Test Data

    String(Source): Test Data
    String(Destination): Test Data
End of the Program....



Program Reference: Cpp_Programs/ Day_05/ Prog_08.cpp


#include<iostream>
#include<string>
#include<iterator>

using namespace std;
//using namespace iterator;

int main (void)
{
//DECLARING OBJECT UNDER THE CLASS string 
  string StrS;
  char StrD[100];
  int i;

  cout<<"\nWorking with String Class....";

//READ A STRING USING getline()
  cout<<"\n\tEnter a String: ";
  getline(cin, StrS); //for string input

//COPY BY ACCESSING EACH CHARACTER INDIVIDUALLY
  cout<<"\n\tCopy using Index Access:";
  for (i=0; i<StrS.length(); i++) StrD[i]=StrS.at(i);
  StrD[i]= '\0';
  cout<<"\n\t\tString(Source): "<<StrS<<"\n\t\tCharacter Array(Destination): "<<StrD;

//COPY USING LIBRARY FUNCTION
  cout<<"\n\n\tCopy using Library Function:";
  StrS.copy(StrD, StrS.length(), 0);
  cout<<"\n\t\tString(Source): "<<StrS<<"\n\t\tCharacter Array(Destination): "<<StrD;

  cout<<"\nEnd of the Program....\n\n";

}


Output

Working with String Class....
    Enter a String: Test Data

    Copy using Index Access:
        String(Source): Test Data
        Character Array(Destination): Test Data

    Copy using Library Function:
        String(Source): Test Data
        Character Array(Destination): Test Data
End of the Program....



Program Reference: Cpp_Programs/ Day_05/ Prog_09.cpp

#include<iostream>
#include<string>
#include<iterator>

using namespace std;
//using namespace iterator;

int main (void)
{
//DECLARING OBJECT UNDER THE CLASS string 
  string Str1, Str2;
  int i;

  cout<<"\nWorking with String Class....";

  cout<<"\n\tEnter a String: ";
  getline(cin, Str1); //for string input

  cout<<"\tEnter another String: ";
  getline(cin, Str2); //for string input

  cout<<"\n\tBefore Swaping:";
  cout<<"\n\t\tFirst String: "<<Str1;
  cout<<"\n\t\tSecond String: "<<Str2;

  Str1.swap(Str2);

  cout<<"\n\n\tAfter Swaping:";
  cout<<"\n\t\tFirst String: "<<Str1;
  cout<<"\n\t\tSecond String: "<<Str2;

  cout<<"\nEnd of the Program....\n\n";

}


Output
Working with String Class....
    Enter a String: This is First String
    Enter another String: This is Second String

    Before Swaping:
        First String: This is First String
        Second String: This is Second String

    After Swaping:
        First String: This is Second String
        Second String: This is First String
End of the Program....