DSA
LANGUAGE OPTED - C++
DAY-2 CHAR ARRAY AND STRINGS
1. Reverse a String
#include <iostream>
#include <algorithm> // For std::reverse
#include <cstring> // For strlen
#include <algorithm> // For std::reverse
#include <cstring> // For strlen
int main() {
char str[] = "Hello, World!";
int n = strlen(str);
// Using std::reverse
std::reverse(str, str + n);
std::cout << "Reversed string: " << str << std::endl;
return 0;
}
2. ASCII VALUES
a-z 97-122
A-Z 65 to 90
space 32
# 35
= 61
@ 64
Click here to check out all ASCII Values
3.CASE CONVERSION
4. Check Pallindrome :
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char ch[1000];
cout << "Enter a String: ";
cin.getline(ch, 1000);
int n = strlen(ch);
int l = 0, r = n - 1;
bool isPalindrome = true;
while (l <= r) {
if (ch[l] != ch[r]) {
isPalindrome = false;
break;
}
l++;
r--;
}
if (isPalindrome) {
cout << "Palindrome" << endl;
} else {
cout << "Not Palindrome" << endl;
}
return 0;
}
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int main() {
char ch[1000];
cout << "Enter the string: ";
cin.getline(ch, 1000);
// Store the original string in temp
char temp[1000];
strcpy(temp, ch);
// Get the length of the string
int n = strlen(ch);
// Reverse the string
reverse(ch, ch + n);
cout << "The reversed string is: " << ch << endl;
// Check if the original string (temp) is equal to the reversed string (ch)
if (strcmp(temp, ch) == 0) {
cout << "It's a palindrome." << endl;
} else {
cout << "It's not a palindrome." << endl;
}
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
int main() {
char ch[1000];
cout << "Enter the string: ";
cin.getline(ch, 1000);
int n = strlen(ch);
bool isPalindrome = true;
for (int i = 0; i < n / 2; i++) {
if (ch[i] != ch[n - i - 1]) {
isPalindrome = false;
break;
}
}
if (isPalindrome) {
cout << "It's a palindrome." << endl;
} else {
cout << "It's not a palindrome." << endl;
}
return 0;
}
bool checkPalindrome(char ch[], int n) {
//n -> length of string
int i=0;
int j = n-1;
while( i<= j) {
if(ch[i] == ch[j]) {
i++;
j--;
}
else {
//characters are not matching
return false;
}
}
//agar yaha pohjoch gye ho
//iska matlab saare characters match kr gye h
///iska matlab palindrome hai
//iska matlab return true krdo
return true;
}


Comments
Post a Comment
Feel Free to say
Confession !!!!