← Back to DSA Course | Chapter 3: Strings | Lesson 1 of 6

String परिचय

एक string एक bead necklace जैसा है जहां हर bead एक letter है। order में strung किए गए वे words, names और sentences बनाते हैं।
Syntax
markup
text = "string value"
length = len(text)
first_char = text[0]
last_char = text[-1]

What is a String?

एक string characters का एक ordered sequence है, text represent करने के लिए उपयोग होता है जैसे names, sentences, या file paths। ज़्यादातर languages में, strings read-only या specially-managed sequences की तरह behave करते हैं अपने खुद के built-in operations के set के साथ।

उदाहरण: What is a String?

#include <iostream>
using namespace std;
int main() {
    string s = "hello";
    cout << "String: " << s << ", type holds ordered characters" << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        String s = "hello";
        System.out.println("String: " + s + ", type holds ordered characters");
    }
}
s = "hello"
print("String:", s, ", type holds ordered characters")
#include <stdio.h>
int main() {
    char s[] = "hello";
    printf("String: %s, type holds ordered characters\n", s);
    return 0;
}

String Length

एक string की length बिल्कुल बताती है कि इसमें कितने characters हैं, जो loops लिखने के लिए ज़रूरी है जो bounds के अंदर रहें और input validate करने के लिए, जैसे यह जांचना कि एक password minimum length पूरी करता है।

उदाहरण: String Length

#include <iostream>
using namespace std;
int main() {
    string s = "password123";
    cout << "Length: " << s.length() << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        String s = "password123";
        System.out.println("Length: " + s.length());
    }
}
s = "password123"
print("Length:", len(s))
#include <stdio.h>
#include <string.h>
int main() {
    char s[] = "password123";
    printf("Length: %lu\n", strlen(s));
    return 0;
}

Access Characters

किसी string में हर character की एक position होती है जिसे इसका index कहते हैं, और ज़्यादातर languages में वह indexing 0 से शुरू होती है, इसलिए length n वाली एक string में 0 से n−1 तक valid indexes होते हैं, बिल्कुल एक array की तरह।

उदाहरण: Access Characters

#include <iostream>
using namespace std;
int main() {
    string s = "hello";
    cout << "s[0]=" << s[0] << ", s[4]=" << s[4] << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        String s = "hello";
        System.out.println("s.charAt(0)=" + s.charAt(0) + ", s.charAt(4)=" + s.charAt(4));
    }
}
s = "hello"
print(f"s[0]={s[0]}, s[4]={s[4]}")
#include <stdio.h>
int main() {
    char s[] = "hello";
    printf("s[0]=%c, s[4]=%c\n", s[0], s[4]);
    return 0;
}

Read a String

Programs आमतौर पर string input को एक user, एक file, या एक network request से पढ़ते हैं, और exact method (जैसे Python में input() या Java में Scanner) language और text कहां से आ रहा है इस पर निर्भर करता है।

उदाहरण: Read a String

#include <iostream>
#include <sstream>
using namespace std;
int main() {
    istringstream input("Alice"); // stands in for real cin >> s
    string s;
    input >> s;
    cout << "Read: " << s << endl;
    return 0;
}
import java.util.Scanner;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner("Alice"); // stands in for new Scanner(System.in)
        String s = sc.next();
        System.out.println("Read: " + s);
    }
}
s = "Alice"  # stands in for input()
print("Read:", s)
#include <stdio.h>
int main() {
    char s[20];
    sscanf("Alice", "%s", s); // stands in for scanf("%s", s)
    printf("Read: %s\n", s);
    return 0;
}

Basic String Practice

अच्छे starting exercises हैं यह count करना कि एक character कितनी बार दिखता है, या हर character को इसके index के साथ print करना, क्योंकि ये उसी indexing skills बनाते हैं जो आपको ज़्यादा advanced string algorithms के लिए चाहिए होंगी।

उदाहरण: Basic String Practice

#include <iostream>
using namespace std;
int main() {
    string s = "banana";
    int count = 0;
    for (char c : s) if (c == 'a') count++;
    cout << "'a' appears " << count << " times" << endl;
    return 0;
}
public class Main {
    public static void main(String[] args) {
        String s = "banana";
        int count = 0;
        for (char c : s.toCharArray()) if (c == 'a') count++;
        System.out.println("'a' appears " + count + " times");
    }
}
s = "banana"
print("'a' appears", s.count('a'), "times")
#include <stdio.h>
#include <string.h>
int main() {
    char s[] = "banana";
    int count = 0;
    for (int i = 0; i < strlen(s); i++) if (s[i] == 'a') count++;
    printf("'a' appears %d times\n", count);
    return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. s[s.length()] access करना, जब आखिरी valid index length() - 1 है।
  2. s.length() को एक fixed array size के साथ confuse करना, और यह भूल जाना कि C++ string इसकी length में कोई visible terminating \0 store नहीं करता।
  3. cin >> s से एक पूरी line पढ़ना, जो पहले space पर रुकता है (getline उपयोग करें)।
🔒

Chapter Quiz — Complete all 6 topics to unlock

0/6 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.