पैलिंड्रोम जाँचें
जाँचें कि कोई शब्द दोनों दिशाओं से एक जैसा पढ़ा जाता है या नहीं।
पैलिंड्रोम जाँचें क्या है?
पैलिंड्रोम अक्षरों का क्रम उलटने पर भी नहीं बदलता।
महत्वपूर्ण बातें
- Compiler warnings चालू करके सभी ठीक करें।
- हर value का type और lifetime समझें।
- Input और array bounds validate करें।
C और C++ code examples
main.c
#include <stdio.h>
#include <string.h>
int main(void) {
const char *text = "level"; size_t length = strlen(text); int palindrome = 1;
// Compare characters mirrored around the center.
for (size_t i = 0; i < length / 2; ++i)
if (text[i] != text[length - 1 - i]) { palindrome = 0; break; }
printf("%s is %sa palindrome\n", text, palindrome ? "" : "not ");
}
level is a palindrome
C++
main.cpp
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string text = "level";
// equal compares the first half with a reversed view.
bool palindrome = std::equal(text.begin(), text.begin() + text.size() / 2, text.rbegin());
std::cout << text << " is " << (palindrome ? "" : "not ") << "a palindrome\n";
}
level is a palindrome
C और C++ की तुलना
आमने-सामने की जगहों की तुलना करें और बीच में रुकें, क्योंकि हर जोड़ी एक बार ही जाँचनी है।
C
C छोटे procedural APIs और representation details स्पष्ट करता है।
C++
C++ low-level model रखकर safer library types जोड़ता है।
अभ्यास
दोनों versions चलाकर बदलें और language guarantees की तुलना करें।
- Invalid और boundary inputs जोड़ें।
- -Wall -Wextra -Wpedantic से compile करें।
- Declarations और implementation अलग करें।
चैलेंज से अभ्यास करें
इस पाठ में सीखी बात से एक असली समस्या हल करें। हर चैलेंज ब्राउज़र में चलता है, तुरंत टेस्ट और AI रिव्यू के साथ।
सभी चैलेंज