B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP C++ Strings | Login Technologies

C++ Strings

In C++, strings are objects of the std::string class that store text. They provide many built-in functions.

C++ में strings, std::string class के objects होते हैं जो text store करते हैं। इनमें कई built-in functions होते हैं।

Example 1: Declare and Print String
#include <iostream>
#include <string>

int main() {
  std::string str = "Hello, World!";
  std::cout << str;
  return 0;
}

Output: Hello, World!

Prints the string using std::cout.

std::cout का use करके string print करता है।

Example 2: Get String Length
#include <iostream>
#include <string>

int main() {
  std::string str = "Hello";
  std::cout << "Length = " << str.length();
  return 0;
}

Output: Length = 5

Uses length() method to get string length.

length() method से string की length निकालता है।

Example 3: Copy String
#include <iostream>
#include <string>

int main() {
  std::string src = "Login Technologies";
  std::string dest = src;
  std::cout << dest;
  return 0;
}

Output: Login Technologies

Copies one string to another using assignment.

assignment operator से string copy करता है।

Example 4: Concatenate Strings
#include <iostream>
#include <string>

int main() {
  std::string str1 = "Hello ";
  std::string str2 = "World!";
  std::string result = str1 + str2;
  std::cout << result;
  return 0;
}

Output: Hello World!

Concatenates two strings using + operator.

+ operator से दो strings जोड़ता है।

Example 5: Compare Strings
#include <iostream>
#include <string>

int main() {
  std::string str1 = "apple";
  std::string str2 = "apple";
  if(str1 == str2)
    std::cout << "Strings are equal";
  else
    std::cout << "Strings are not equal";
  return 0;
}

Output: Strings are equal

Checks if strings are equal with == operator.

== operator से strings की equality चेक करता है।

Example 6: Get Input String with Spaces
#include <iostream>
#include <string>

int main() {
  std::string str;
  std::cout << "Enter a line: ";
  std::getline(std::cin, str);
  std::cout << "You entered: " << str;
  return 0;
}

Output: (User input shown)

Uses getline() to read a line with spaces.

getline() से space सहित पूरी line लेता है।

Example 7: Access Characters in String
#include <iostream>
#include <string>

int main() {
  std::string str = "Hello";
  for(char c : str) {
    std::cout << c << std::endl;
  }
  return 0;
}

Output: H E L L O (each on new line)

Loops through each character in the string.

string के हर character को print करता है।

Example 8: Find Substring
#include <iostream>
#include <string>

int main() {
  std::string str = "Login Technologies";
  size_t pos = str.find("Tech");
  if(pos != std::string::npos)
    std::cout << "Substring found at position " << pos;
  else
    std::cout << "Substring not found";
  return 0;
}

Output: Substring found at position 6

Finds substring position using find().

find() से substring की position बताता है।

Example 9: Erase Part of String
#include <iostream>
#include <string>

int main() {
  std::string str = "Hello World";
  str.erase(5, 6); // remove " World"
  std::cout << str;
  return 0;
}

Output: Hello

Uses erase() to remove part of string.

erase() से string का हिस्सा हटाता है।

Example 10: Reverse String Manually
#include <iostream>
#include <string>

int main() {
  std::string str = "Hello";
  for(int i = str.length() - 1; i >= 0; i--) {
    std::cout << str[i];
  }
  return 0;
}

Output: olleH

Prints string in reverse order using a loop.

loop से string को उल्टा print करता है।