शुरुआती C/C++ अभ्यास

स्ट्रिंग उलटें

अक्षर बदले बिना टेक्स्ट उलटें।

स्ट्रिंग उलटें क्या है?

स्ट्रिंग उलटना इंडेक्स और उसी जगह अदला-बदली का अच्छा अभ्यास है।

महत्वपूर्ण बातें

  • Compiler warnings चालू करके सभी ठीक करें।
  • हर value का type और lifetime समझें।
  • Input और array bounds validate करें।

C और C++ code examples

C
कोड चलाएँ →
main.c
#include <stdio.h>
#include <string.h>
int main(void) {
    char text[] = "CodeUtility"; size_t length = strlen(text);
    // Swap mirrored characters in place.
    for (size_t i = 0; i < length / 2; ++i) {
        char temporary = text[i]; text[i] = text[length - 1 - i]; text[length - 1 - i] = temporary;
    }
    puts(text);
}
अपेक्षित output
ytilitUedoC
C++
कोड चलाएँ →
main.cpp
#include <algorithm>
#include <iostream>
#include <string>
int main() {
    std::string text = "CodeUtility";
    // reverse applies swaps across the selected range.
    std::reverse(text.begin(), text.end());
    std::cout << text << '\n';
}
अपेक्षित output
ytilitUedoC

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 रिव्यू के साथ।

सभी चैलेंज