C String Functions - w3 coding club

    String से संबंधित function

    किसी संख्या पर कार्य करने के लिए जो विभिन्न  ऑपरेटर्स(operators) जैसे ==, +, <, > आदि उपलब्ध है। किंतु जब इसी प्रकार के कार्य string(स्ट्रिंग) पर किए जाने हो तो इन operators(ऑपरेटर्स)  को प्रयोग नहीं किया जा सकता हैं। 

    किसी स्ट्रिंग की तुलना दूसरी स्ट्रिंग से  करनी हो या स्ट्रिंग से  कितने tractors(करेक्टर्स) है, यह गणना करनी हो यादव स्ट्रिंग को मिलाकर एक करना हो, तो इसके लिए हमारे पास विभिन्न function(फंक्शन) है जो यह कार्य करने में सक्षम हैं। 

    स्ट्रिंग फंक्शन (string function) को प्रयोग में लेने के लिए प्रोग्राम के प्रारंभ में string.h को #include करना चाहिए। 

    स्टिंग से संबंधित मुख्य फंक्शन निम्न प्रकार है:-

    (1) strlen():-

    इस function(फंक्शन)  का प्रयोग किसी string में उपस्थित characters(अक्षरों) की संख्या ज्ञात करने के लिए किया जाता हैं। 


       #include <stdio.h>
       #include <conio.h>
       #include <string.h>
       void main()
       {
          char nm[50= "Nirma";
          int len = strlen(nm);
          printf("Lenghth = %d",len);   
          getch();
       }

    Output:-

    Lenghth = 5

    (2) strlwr():-

    इस फंक्शन का प्रयोग बड़े अक्षरों(Capital) को छोटे अक्षरों(Small) में परिवर्तित करने के लिए किया जाता हैं। 


       #include <stdio.h>
       #include <conio.h>
       #include <string.h>
       void main()
       {
          char nm[50= "MUKESH KUMAR";
          printf("\nbefore : %s", nm);
          strlwr(nm);
          printf("\nafter  : %s", nm);
          getch();
       }

    Output:-

    before : MUKESH KUMAR
    after  : mukesh kumar

    (3) strupr():-

    फंक्शन का प्रयोग छोटे अक्षरों(Small) को  बड़े अक्षरों(Capital) में परिवर्तित करने के लिए किया जाता हैं।  


       #include <stdio.h>
       #include <conio.h>
       #include <string.h>
       void main()
       {
          char nm[50= "nirma kanwar";
          printf("\nbefore : %s", nm);
          strupr(nm);
          printf("\nafter  : %s", nm);
          getch();
       }

    Output:-

    before : nirma kanwar
    after  : NIRMA KANWAR

    (4) strcat():-

    इस फंक्शन का प्रयोग दो स्टिंग्स को आपस में concatenate(जोड़ने)  के लिए किया जाता हैं। 


       #include <stdio.h>
       #include <conio.h>
       #include <string.h>
       void main()
       {
          char nm[10= "Mukesh ";
           char mn[10= " Kumar";
          strcat(nm, mn);
          printf("%s",nm);
          getch();
       }

    Output:-

    Mukesh  Kumar

    (5) strncat():-

    इस function(फंक्शन) का प्रयोग दो  string(स्ट्रिंग) को आपस में निश्चित अक्षरों(characters) तक concatenate(जोड़ने) के लिए किया जाता हैं। 


       #include <stdio.h>
       #include <conio.h>
       #include <string.h>
       void main()
       {
          char nm[] = "Mukesh ";
          char mn[] = "Kumar";
          strncat(nm, mn,1);
          printf("%s",nm);
          getch();
       }

    Output:-

    Mukesh K

    (6) strcpy():-

    इस function(फंक्शन) का प्रयोग एक स्प्रिंगstring(स्ट्रिंग) को दूसरे variable(वेरिएबल ) में कॉपी करने के लिए किया जाता हैं। 


       #include <stdio.h>
       #include <conio.h>
       #include <string.h>
       void main()
       {
          char n1[10= "Mukesh ";
          char n2[10];
          strcpy(n2,n1);
          printf("\n String 1 : %s",n1);
          printf("\n String 2 : %s",n2);
          getch();
       }

    Output:-

    String 1 : Mukesh
    String 2 : Mukesh

    (7) strcmp():-

    इस फंक्शन का प्रयोग दो  स्प्रिंग की तुलना करने के लिए किया जाता हैं। यदि दोनों स्ट्रिंग समान होती है तो यह फंक्शन 0 रिटर्न करता हैं। अन्यथा उनके ASCLL Code के अंतर को रिटर्न करता हैं। 

    यह फंक्शन वास्तव में स्टिंग के प्रत्येक कैरेक्टर की तुलना करता है तथा जहां पर भी असमान कैरेक्टर मिलते हैंं उन कैरेक्टर के ASCLL Code  के अंतर को रिटर्न करता हैं। उदाहरण के लिए यदि दो स्ट्रिंग क्रमशः Nishu तथा Nirma हैं इसमें प्रारंभ के दो कैरेक्टर Ni समान है तथा तीसरा करैक्टर (s और r) आसमान हैं। 

    चूंकि s का ASCLL Code 115 तथा r का ASCLL Code 114 होता हैं अतः रिटर्न की जाने वाली वैल्यू  115-114 अर्थात 1  होगी हैं। 


        #include <stdio.h>
        #include <conio.h>
        #include <string.h>
        void main()
        {
           char n1[10= "Nirma";
           char n2[10= "Nishu";
           printf("\n %d"strcmp(n1, n2));
           printf("\n %d"strcmp(n2, n1));        
           getch();
        }

    Output:-

    -1
    1

    (8) strcmpi():-

    इस function(फंक्शन) का प्रयोग दो spring  की तुलना करने के लिए किया जाता हैं किंतु इसमें छोटे(small) और बड़े अक्षरों(capital letter) को अलग-अलग नहीं  माना जाता हैं। अन्य शब्दों में यह केस  इनसेंसेटिव(case insensitive) हैं। 


       #include <stdio.h>
       #include <conio.h>
       #include <string.h>
       void main()
       {
          char n1[10= "Mukesh";
          char n2[10= "mukesh";
          printf("\n %d"strcmpi(n1, n2));
          printf("\n %d"strcmpi(n2, n1));       
          getch();
       }

    Output:-

    0
    0

    (9) strrev():-

    इस function(फंक्शन) का प्रयोग  sting(स्ट्रिंग) को विपरीत क्रम में प्रयोग करने के लिए किया जाता हैं। 


       #include <stdio.h>
       #include <conio.h>
       #include <string.h>
       void main()
       {
          char n1[10= "MUKESH";
          printf("\nBefore : %s",n1);      
          strrev(n1);
          printf("\nAfter : %s",n1);
          getch();
       }

    Output:-

    Before : MUKESH
    After : HSEKUM

    https://www.w3codingclub.com/

    Post a Comment

    0 Comments