C/C++ की मूल बातें

C और C++ में स्ट्रिंग

C टेक्स्ट को char ऐरे में रखता है जो छिपे '\0' कैरेक्टर पर खत्म होता है, जबकि C++ उसे std::string में लपेटता है, जो मेमोरी खुद संभालता है।

C और C++ में स्ट्रिंग क्या है?

C टेक्स्ट को char ऐरे में रखता है जो छिपे '\0' कैरेक्टर पर खत्म होता है, जबकि C++ उसे std::string में लपेटता है, जो मेमोरी खुद संभालता है।

C के कैरेक्टर ऐरे और C++ के std::string की तुलना करें: लंबाई, जोड़ना, तुलना और खोज।

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

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

C और C++ code examples

C
कोड चलाएँ →
main.c
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void) {
    /* A C string is a char array that ends with '\0'. */
    char name[32] = "Ada";
    printf("%s has %zu letters\n", name, strlen(name));

    /* strcat appends; the array must have room for the result. */
    strcat(name, " Lovelace");
    printf("%s\n", name);

    /* strcmp returns 0 when two strings are equal. */
    printf("equal: %d\n", strcmp("cat", "cat") == 0);

    /* Walk the characters until the terminating '\0'. */
    char upper[32];
    size_t i = 0;
    for (; name[i] != '\0'; i++) {
        upper[i] = (char)toupper((unsigned char)name[i]);
    }
    upper[i] = '\0';
    printf("%s\n", upper);

    /* strstr finds a substring and returns a pointer to it, or NULL. */
    printf("contains \"love\": %s\n", strstr("lovelace", "love") ? "yes" : "no");
    return 0;
}
अपेक्षित output
Ada has 3 letters
Ada Lovelace
equal: 1
ADA LOVELACE
contains "love": yes
C++
कोड चलाएँ →
main.cpp
#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>

int main() {
    // std::string manages its own memory and grows as needed.
    std::string name = "Ada";
    std::cout << name << " has " << name.size() << " letters\n";

    name += " Lovelace";
    std::cout << name << '\n';

    // == compares the contents of two strings.
    std::cout << "equal: " << (std::string("cat") == "cat") << '\n';

    std::string upper = name;
    std::transform(upper.begin(), upper.end(), upper.begin(),
                   [](unsigned char c) { return static_cast<char>(std::toupper(c)); });
    std::cout << upper << '\n';

    // find returns std::string::npos when the text is missing.
    std::string word = "lovelace";
    std::cout << "contains \"love\": " << (word.find("love") != std::string::npos ? "yes" : "no") << '\n';
    std::cout << "first name: " << name.substr(0, 3) << '\n';
}
अपेक्षित output
Ada has 3 letters
Ada Lovelace
equal: 1
ADA LOVELACE
contains "love": yes
first name: Ada

C और C++ की तुलना

C में strlen, strcat, strcmp और strstr सीधे ऐरे पर काम करते हैं, और ऐरे काफ़ी बड़ा है यह आपको खुद देखना होता है। std::string में size(), +=, ==, find() और substr() हैं और यह अपने आप बढ़ता है, जिससे बफ़र ओवरफ़्लो से बचाव होता है।

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

सभी चैलेंज