STRINGS
String
1. length() or size(): Returns the length of the string
2. array[index]: Access individual characters using array indexing
at(): Access a character at a specified index
3. + Operator: Concatenates two strings
4. append(): Adds one string to the end of another
5. == Operator: Compares two strings for equality
6. compare(): Returns an integer indicating the comparison result
7. substr(): Extracts a substring from a string
8. find(): Returns the position of the first occurrence of a substring
9. replace(): Modifies a part of the string
10. insert(): Adds a substring at a specified position
11. erase(): Removes a part of the string
String
// String Length // String Comparison
string text = "Welcome to C++ Program"; std::string str1 = "Welcome to C++";
int length = text.length(); std::string str2 = "Welcome to Java";
int length = text.size(); // length = 22 if (str1 == str2)
std::cout << "Strings are equal";
// Accessing Characters else
cout << text.at(3); // Output: c std::cout << "Strings are not equal"; // Output: Strings
are not equal
// Concatenating Strings
string s1 = "Welcome"; // compare()
string s2 = " to C++ Program"; std::string s1 = "Welcome to C++";
string s3 = s1 + s2; // "Welcome to C++ Program" std::string s2 = "Welcome to C++ Program";
int result = s1.compare(s2); // result < 0
// append()
string base = "Welcome"; // Searching - find()
base.append(" to C++ Program"); // "Welcome to C++ string s5 = "Welcome to C++ Program";
Program" int position = s5.find("C++"); // position = 11
String
// Generate Substring - substr()
string s5 = "Welcome to C++ Program";
string sub = s5.substr(11, 3); // sub = "C++"
// Modifying Strings - insert()
string s6 = "Welcome to C++ Program";
s6.insert(11, " powerful"); // "Welcome to powerful C++ Program"
// replace()
s6.replace(11, 9, "amazing"); // "Welcome to amazing C++ Program"
// erase()
s6.erase(11, 10); // "Welcome to Program"
String
getline(cin, variable_name);
cin → Standard input stream (keyboard input).
variable_name → A string variable where the input will be stored.
#include <iostream>
#include <string>
using namespace std;
int main() {
string mystr;
cout << "Enter your full name: ";
getline(cin, mystr);
cout << mystr << endl;
return 0;
}
String
#include <iostream>
// total lenghth of str1 after concatenation
#include <cstring>
len = strlen(str1);
using namespace std;
cout << "strlen(str1) : " << len << endl;
int main ()
return 0;
{
}
char str1[10] = "Hello";
char str2[10] = "World";
char str3[10];
int len ;
// copy str1 into str3
strcpy( str3, str1);
cout << "strcpy( str3, str1) : " << str3 << endl;
// concatenates str1 and str2
strcat( str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;
String
#include <iostream> #include <iostream>
#include <stdio.h> using namespace std; #include <string>
using namespace std;
int main() { int main() {
char str[50]; char str[50]; int main() {
printf("Enter a string: "); cout << "Enter a sentence: "; string str;
gets(str); gets(str); cout << "Enter a string: ";
printf("You entered: %s\n", str); cin.getline(str, 50); getline(cin, str);
return 0; cout << str << endl; cout <<str << endl;
} //works in c compiler return 0; return 0;
} }
cin.getline(char* str, int size, char delimiter = '\n'); getline(istream& input, string& str, char delimiter = '\n');
cin.getline(str, 50, ',');
String
#include <iostream>
#include <cstdio>
using namespace std;
int main() {
char str[50];
cin.getline(str,50);
puts(str);
return 0;
}
String
#include <iostream>
#include <cctype> // Required for toupper() #include <iostream>
using namespace std; #include <cctype> // Required for toupper()
using namespace std;
int main() {
char lower = 'a'; int main() {
string str = "hello";
char upper = toupper(lower);
cout << "Uppercase of '" << lower << "': " << upper // Capitalize first letter
<< endl; str[0] = toupper(str[0]);
return 0; cout << "Capitalized String: " << str << endl;
} return 0;
}
classes
#include<iostream> class B { int main() {
#include <string> private: string str1;
#include <cstring> char s[100]; char str2[100];
using namespace std; public: cout << "Enter a string for class A: ";
class A { B( char str[]) { cin >> str1;
private: strcpy(s, str); cout << "Enter a string for class B: ";
string s; } cin >> str2;
public: bool isPalindrome() { A objA(str1);
A(string str) { int len = strlen(s); B objB(str2);
s = str; for (int i = 0; i < len / 2; i++) { cout << (objA.isPalindrome() ? "Yes" : "No")
} if (s[i] != s[len - 1 - i]) << "\n";
bool isPalindrome() { return false; cout << (objB.isPalindrome() ? "Yes" : "No")
int len = s.length(); } << "\n";
for (int i = 0; i < len / 2; i++) { return true; return 0;
if (s[i] != s[len - 1 - i]) } }
return false; };
}
return true;}};