Write a C++ program for find the Length of the string without using strlen() function.
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
int length = 0;
cout << "Enter a string: "
<< endl;
getline(cin, str);
for (int i = 0; str[i] != '\0'; i++)
{
length++;
}
// or we
can write but que asking without length() or size()
// length = str.length();
cout << "Length of string: "
<< length << endl;
return 0;
}
Write a program to check the strength of a password as Weak.
#include <iostream>
using namespace std;
int main() {
string
password;
cout
<< "Enter a password: ";
cin
>> password;
int
hasUpper = 0, hasLower = 0, hasDigit = 0, hasSpecial = 0;
for (int
i=0; i<password.size(); i++)
{
if
(password[i] >= 'A' && password[i] <= 'Z')
hasUpper
= 1;
else
if (password[i] >= 'a' && password[i] <= 'z')
hasLower
= 1;
else
if (password[i] >= '0' && password[i] <= '9')
hasDigit
= 1;
else
hasSpecial = 1;
}
if
(password.length() >= 8 && hasUpper && hasLower &&
hasDigit && hasSpecial) {
cout
<< "Password is Strong" << endl;
} else {
cout
<< "Password is Weak" << endl;
}
return
0;
}
Write a program to check the strength of a password as Weak, Average, or
Strong based on specific criteria (e.g., length, special characters, and case
sensitivity).
Criteria:
- Weak: Password is less
than 8 characters or lacks any of the following: uppercase, lowercase,
digit, or special character.
- Average: Password is at
least 8 characters and meets at least two of the criteria (uppercase, lowercase,
digit, special character).
- Strong: Password is at
least 8 characters and meets all
four criteria.
#include <iostream>
#include <string>
using namespace std;
int main() {
string
password;
cout
<< "Enter a password: ";
cin
>> password;
bool
hasUpper = false, hasLower = false, hasDigit = false, hasSpecial = false;
for (int
i = 0; i < password.size(); i++)
{
if
(password[i] >= 'A' && password[i] <= 'Z')
hasUpper = true;
else
if (password[i] >= 'a' && password[i] <= 'z')
hasLower = true;
else
if (password[i] >= '0' && password[i] <= '9')
hasDigit = true;
else
hasSpecial = true;
}
// Check
for criteria
if
(password.length() >= 8 && hasUpper && hasLower &&
hasDigit && hasSpecial)
{
cout
<< "Password is Strong" << endl;
} else
{
cout
<< "Password is Weak. Suggestions:" << endl;
if
(password.length() < 8)
cout << "- Password must be at least 8 characters long."
<< endl;
if
(!hasUpper)
cout << "- Include at least one uppercase letter (A-Z)."
<< endl;
if
(!hasLower)
cout << "- Include at least one lowercase letter (a-z)."
<< endl;
if
(!hasDigit)
cout << "- Include at least one digit (0-9)." <<
endl;
if
(!hasSpecial)
cout << "- Include at least one special character (e.g., !,
@, #, $, etc.)." << endl;
}
return
0;
}
Write a program to sort any string in increasing or decreasing order based
on user input.
#include <iostream>
using namespace std;
void bubbleSort(string &str, bool ascending) {
int n =
str.size();
for (int
i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if ((ascending && str[j] > str[j + 1]) || (!ascending
&& str[j] < str[j + 1])) {
char temp = str[j];
str[j] = str[j + 1];
str[j + 1] = temp;
}
}
}
}
int main() {
string
str;
char
choice;
cout
<< "Enter a string: ";
cin
>> str;
cout
<< "Choose sorting order (I for Increasing, D for Decreasing):
";
cin
>> choice;
if
(choice == 'I' || choice == 'i') {
bubbleSort(str, true);
cout
<< "String in increasing order: " << str << endl;
}
else if
(choice == 'D' || choice == 'd') {
bubbleSort(str, false);
cout
<< "String in decreasing order: " << str << endl;
}
else {
cout
<< "Invalid choice. Please enter I or D." << endl;
}
return
0;
}
Write a program to count spaces, words, and sentences from a string fetched
from a file.
#include <iostream>
using namespace std;
int main() {
string
text;
cout
<< "Enter a string: ";
getline(cin, text);
int
spaces = 0, words = 0, sentences = 0;
for (int
i = 0; i < text.size(); i++)
{
if
(text[i] == ' ')
spaces++;
else
if (text[i] == '.' || text[i] == '!' || text[i] == '?')
sentences++;
}
words =
spaces + 1;
cout
<< "Spaces: " << spaces << endl;
cout
<< "Words: " << words << endl;
cout
<< "Sentences: " << sentences << endl;
return
0;
}
Write a program to reverse a given string.
#include <iostream>
using namespace std;
int main() {
string
str, reversed;
cout
<< "Enter a string: ";
cin
>> str;
int n =
str.size();
for (int
i = n - 1; i >= 0; i--) {
reversed += str[i];
}
cout
<< "Reversed string: " << reversed << endl;
return
0;
}
Write a program to check whether a given number or string is a palindrome.
#include <iostream>
using namespace std;
bool isPalindromeString(string str) {
int
start = 0, end = str.size() - 1;
while
(start < end) {
if
(str[start] != str[end]) {
return false;
}
start++;
end--;
}
return
true;
}
bool isPalindromeNumber(int num) {
int
original = num, reversed = 0;
while
(num > 0) {
int
digit = num % 10;
reversed = reversed * 10 + digit;
num
/= 10;
}
return
original == reversed;
}
int main() {
cout
<< "Enter '1' to check a number or '2' to check a string: ";
int
choice;
cin
>> choice;
if
(choice == 1) {
int
num;
cout
<< "Enter a number: ";
cin
>> num;
if
(isPalindromeNumber(num)) {
cout << num << " is a palindrome number." <<
endl;
}
else {
cout << num << " is not a palindrome number."
<< endl;
}
} else
if (choice == 2) {
string str;
cout
<< "Enter a string: ";
cin
>> str;
if
(isPalindromeString(str)) {
cout << str << " is a palindrome string." <<
endl;
}
else {
cout << str << " is not a palindrome string."
<< endl;
}
} else {
cout
<< "Invalid choice!" << endl;
}
return
0;
}
Reverse a String: Input a string and reverse its characters.
#include
<iostream>
using
namespace std;
int
main() {
string str;
getline(cin,str);
int n = str.length();
for (int i = 0; i < n / 2; i++) {
char temp = str[i];
str[i] = str[n - i - 1];
str[n - i - 1] = temp;
}
cout << str;
return 0;
}
Check for Palindrome: Determine if a string is a palindrome.
#include
<iostream>
using
namespace std;
int
main() {
string str;
cin >> str;
int n = str.length();
bool isPalindrome = true;
for (int i = 0; i < n / 2; i++) {
if (str[i] != str[n - i - 1]) {
isPalindrome = false;
break;
}
}
if (isPalindrome)
cout << "Palindrome";
else
cout << "Not a
Palindrome";
return 0;
}
Count Vowels and
Consonants: Count the number of vowels and consonants in a string.
#include
<iostream>
using
namespace std;
int
main() {
string str;
getline(cin,str);
int vowels = 0, consonants = 0;
for (char c : str) {
char ch = tolower(c);
if ((ch >= 'a' && ch <=
'z')) {
if (ch == 'a' || ch == 'e' || ch ==
'i' || ch == 'o' || ch == 'u')
vowels++;
else
consonants++;
}
}
cout << "Vowels: " <<
vowels << endl;
cout << "Consonants: "
<< consonants << endl;
return 0;
}
F ind String Length:
Implement your own function to calculate the length of a string without using
strlen().
#include
<iostream>
using
namespace std;
int
stringLength(string str) {
int length = 0;
for (char c : str)
{
length++;
}
return length;
}
int
main() {
string str;
getline(cin,str);
cout << "Length of the string:
" << stringLength(str) << endl;
return 0;
}
Comments
Post a Comment