🟡 Intermediate  ·  Lesson 23

String Functions

String Functions Overview

C में string functions के लिए <string.h> header file use की जाती है। इससे length निकालना, copy करना, join करना, compare करना और substring search करना आसान हो जाता है।

Important Standard C Note

strrev(), strupr() और strlwr() standard C functions नहीं हैं। ये कुछ compilers में चलते हैं, लेकिन GCC/online compilers में error दे सकते हैं। Portable code के लिए manual loop और <ctype.h> use करें।

Standard String Functions

Functionकाम
strlen(s)String की length देता है
strcpy(d,s)s को d में copy करता है
strcat(d,s)s को d के end में जोड़ता है
strcmp(s1,s2)दो strings compare करता है
strchr(s,ch)character search करता है
strstr(s,sub)substring search करता है

Complete Demo Program

C Language – Standard String Functions
#include <stdio.h>
#include <string.h>
#include <ctype.h>

void reverseString(char s[]) {
    int i = 0, j = strlen(s) - 1;
    while (i < j) {
        char temp = s[i];
        s[i] = s[j];
        s[j] = temp;
        i++;
        j--;
    }
}

void toUpperCase(char s[]) {
    for (int i = 0; s[i] != '\0'; i++) {
        s[i] = toupper((unsigned char)s[i]);
    }
}

int main() {
    char s1[80] = "Namaste";
    char s2[80] = "Duniya";
    char rev[80] = "Code";
    char up[80] = "codingeasily";

    printf("Length = %d\n", (int)strlen(s1));
    strcat(s1, " ");
    strcat(s1, s2);
    printf("Joined = %s\n", s1);
    printf("Compare = %d\n", strcmp("abc", "abc"));

    reverseString(rev);
    printf("Reverse = %s\n", rev);

    toUpperCase(up);
    printf("Uppercase = %s\n", up);
    return 0;
}
Length = 7 Joined = Namaste Duniya Compare = 0 Reverse = edoC Uppercase = CODINGEASILY

Practice

  • String में vowels और consonants count करें।
  • strcmp() के बिना दो strings compare करें।
  • Manual loop से string reverse करें।
  • Sentence में words count करें।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

C में मुख्य string functions क्या हैं?
सबसे इस्तेमाल होने वाले string functions <string.h> में हैं: length के लिए strlen, copy को strcpy, जोड़ने को strcat, तुलना को strcmp, और खोजने को strchr/strstr। ये text को मापने, copy करने, जोड़ने और तुलना करने के आम काम संभालते हैं।
C में string functions के लिए कौन-सी header file चाहिए?
Standard string functions इस्तेमाल करने को आप <string.h> शामिल करते हैं। इसके बिना compiler strlen या strcpy जैसे नाम नहीं पहचानता, तो इन functions के साथ काम करते समय यह header ज़रूरी है।
strcpy और strcat में क्या अंतर है?
strcpy एक string को दूसरी पर copy करता है, उसकी सामग्री बदलते हुए, जबकि strcat एक string को दूसरी के अंत में जोड़ता है, पहला हिस्सा रखते हुए। दोनों को destination इतना बड़ा चाहिए कि परिणाम और null terminator समा सके।
C में strcmp कैसे काम करता है?
strcmp दो strings की character-by-character तुलना करता है और बराबर होने पर 0, पहली छोटी होने पर negative, और बड़ी होने पर positive value लौटाता है। यह आमतौर पर if में यह जाँचने को इस्तेमाल होता है कि दो strings मेल खाती हैं या नहीं।
String functions के लिए destination arrays काफ़ी बड़े क्यों होने चाहिए?
strcpy और strcat जैसे functions destination size नहीं जाँचते, तो वह बहुत छोटा हो तो वे array के अंत के पार लिख देते हैं, memory खराब करते हुए। आपको destination को पूरे परिणाम सहित terminating '\0' रखने लायक बनाना होगा।
← Back to C Tutorial
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.