Guest User

Untitled

a guest
Feb 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #include<iostream> //pgm to implement string functions
  2. #include<string>
  3. using namespace std;
  4. int main()
  5. {
  6. string str1, str2, str3;
  7. char ch, s1[30],s2[30];
  8. int opt,pos,spos,num,found;
  9. cout<<"\nEnter the main string : \n";
  10. gets(s1);
  11. str1=s1;
  12. cout<<"\nEnter the second string : \n";
  13. gets(s2);
  14. str2=s2;
  15. do
  16. {
  17. cout<<"\nEnter option : \n1 - Compare String\n2 - Append String\n3 - Insert String\n4 - Find String\n";
  18. cout<<"5 - Erase String\n6 - Check if substring\n7 - Replace String\n8 - Assign String\n";
  19. cout<<"9 - + String\n10 - = String\n11 - == String\n12 - > String\n13 - < String\n\n";
  20. cin>>opt;
  21. switch(opt)
  22. {
  23. case 1:
  24. if (str1.compare(str2) != 0)
  25. cout <<"\nThe strings are not the same";
  26. else
  27. cout <<"\nBoth the strings are same";
  28. break;
  29. case 2:
  30. str1.append(str2);
  31. cout<<str1;
  32. break;
  33. case 3:
  34. cout<<"\Enter the position for inserting the string : ";
  35. cin>>pos;
  36. str1.insert(pos,str2);
  37. cout<<str1;
  38. break;
  39. case 4:
  40. found=str1.find(str2);
  41. if (found!=string::npos)
  42. cout <<str2<<" found at: " <<int(found)<<endl;
  43. else
  44. cout<<"\nThe string is not found ! ";
  45. break;
  46. case 5:
  47. cout<<"\nEnter the starting position to be erased : ";
  48. cin>>spos;
  49. cout<<"\nEnter the number of letters to be erased : ";
  50. cin>>num;
  51. str1.erase (spos,num);
  52. cout<<"\n"<<str1<<endl;
  53. break;
  54. case 6:
  55. cout<<"\nEnter the starting position of the required substring : ";
  56. cin>>spos;
  57. cout<<"\nEnter the number of letters needed : ";
  58. cin>>num;
  59. str2 = str1.substr (spos,num);
  60. cout<<"\n"<<str2<<endl;
  61. break;
  62. case 7:
  63. cout<<"\nEnter the starting position from which the string is to be replaced : ";
  64. cin>>pos;
  65. cout<<"\nEnter the number of spaces to be replaced : ";
  66. cin>>spos;
  67. str1.replace(pos,spos,str2);
  68. cout<<"\n"<<str1;
  69. break;
  70. case 8:
  71. cout<<"\nEnter the starting position from which the string is to be assigned : ";
  72. cin>>pos;
  73. cout<<"\nEnter the number of spaces to be assigned : ";
  74. cin>>spos;
  75. str2.assign(str1,pos,spos);
  76. cout<<"\n"<<str1;
  77. break;
  78. case 9:
  79. str3=str1+str2;
  80. cout<<"\n"<<str3;
  81. break;
  82. case 10:
  83. str1=str2;
  84. cout<<"\n"<<str1<<endl;
  85. break;
  86. case 11:
  87. if(str1==str2)
  88. cout<<"\nBoth the strings are equal";
  89. else
  90. cout<<"\nBoth the strings are not equal";
  91. break;
  92. case 12:
  93. if(str1>str2)
  94. {
  95. cout<<"\nThe first string is greater than the second!\n";
  96. cout<<str1<<" is greater than "<<str2<<"\n";
  97. }
  98. else if(str2>str1)
  99. {
  100. cout<<"\nThe second string is greater than the first!\n";
  101. cout<<str2<<" is greater than "<<str1<<"\n";
  102. }
  103. else
  104. cout<<"\nEqual";
  105. break;
  106. case 13:
  107. if(str1<str2)
  108. {
  109. cout<<"\nThe first string is smaller than the second!\n";
  110. cout<<str1<<" is smaller than "<<str2<<"\n";
  111. }
  112. else if(str2<str1)
  113. {
  114. cout<<"\nThe second string is greater than the first!\n";
  115. cout<<str2<<" is smaller than "<<str1<<"\n";
  116. }
  117. else
  118. cout<<"\nEqual";
  119. break;
  120. default:
  121. cout<<"\nInvalid option";
  122. }
  123. cout<<"\nDo you want to continue? [y/n] : ";
  124. cin>>ch;
  125. }while(ch=='y'||ch=='Y');
  126. }
Add Comment
Please, Sign In to add comment