- Strings characters का समूह होता है |
- Strings ये One-dimensional array होता है, जिसमे सिर्फ characters होते है |
- String का आखिरी character 'NULL'(\0) होता है |
- अगर पूरा string लिखना हो तो उसे double quotes ( " " ) में लिखा जाता है | अगर एक-एक character को लिखना हो तो उसे single quotes ( ' ' ) में लिखा जाता है |
- String का data type character (char) होता है |
Example for Single Character String
char str1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
Example for Multiple Character String
char str2[6] = "Hello";
Example for Multiple Character String without using Array_size
char str3[] = "Hello";
Source Code :
#include <stdio.h>
int main(){
char str1[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char str2[6] = "Hello";
char str3[] = "Hello";
printf("Value of str1 : %s\n", str1);
printf("Value of str2 : %s\n", str2);
printf("Value of str3 : %s", str3);
return 0;
}
Output :
Value of str1 : Hello Value of str2 : Hello Value of str3 : Hello
String Representation
Note :String में एक से अधिक character होते है इसीलिए '%s' इस fomat specifier का इस्तेमाल करते है | अगर single character को print करना हो तो '%c' इस format specifier का इस्तेमाल किया जाता है |Print String using '%s' format specifier
char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("Value of str : %s\n", str1);
Output :
Value of str : Hello
Print String using '%c' format specifier
Source Code :
char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("Value of str[0] : %c\n", str[0]);
printf("Value of str[1] : %c\n", str[1]);
printf("Value of str[2] : %c\n", str[2]);
printf("Value of str[3] : %c\n", str[3]);
printf("Value of str[4] : %c\n", str[4]);
printf("Value of str[5] : %c\n", str[5]);
Output :
Value of str[0] : H Value of str[1] : e Value of str[2] : l Value of str[3] : l Value of str[4] : o Value of str[5] : NULL Character is not shown
| index of array | str[0] | str[1] | str[2] | str[3] | str[4] | str[5] |
| Array Elements | H | e | l | l | o | \0 |
| Memory Address | 6356728 | 6356732 | 6356736 | 6356740 | 6356744 | 6356748 |
String Working with size (sizeof)
Program में हर एक String के initialization में अलग-अलग size है | दिए हुए array के size की memory allocate की जाती है | अगर Array का size नहीं दिया जाता तो जितनी size string की है उतनी size array allocate करता है |Source Code :
#include <stdio.h>
int main(){
char str1[20] = {'H', 'e', 'l', 'l', 'o', '\0'};
char str2[10] = "Hello"; //using array_size
char str3[] = "Hello"; //without using array_size
printf("Size of of str1 : %d\n", sizeof(str1));
printf("Size of str2 : %d\n", sizeof(str2));
printf("Size of str3 : %d", sizeof(str3));
return 0;
}
Output:
Size of of str1 : 20 Size of str2 : 10 Size of str3 : 6
String using *Pointer
Source Code :
#include <stdio.h>
int main(){
char *ptr = "Hello";
printf("%s\n", ptr);
return 0;
}
Output :
Hello
String Library Functions
इन Functions को Program में इस्तेमाल करना हो तो string.h या strings.h इन header file को include करना पड़ता है || String Function | Description |
|---|---|
| strcat | एक String से दूसरे String को जोड़ा जाता है | |
| strchr | दिए हुए string से एक character का पहला occurrence के आगे का string pointer को return करता है | |
| strcmp | दो String को Compare किया जाता है | ये case-sensetive है | |
| strcmpi | दो String को Compare किया जाता है | ये case-sensetive नहीं है | |
| strcpy | एक string को दूसरे string में copy करता है | |
| strdup | String का duplicate बनाता है | |
| strlen | String की Length निकाली जाती है | |
| strlwr | Uppercase के Characters को Lowercase में convert किया जाता है | |
| strncat | दिए हुए number के जितने character है उनसे String को जोड़ा जाता है | |
| strncpy | दिए हुए number के जितने character एक string से दूसरे string में copy किया जाता है | |
| strnset | दिए हुए number और दिए हुए character के हिसाब से string को replace करता है | |
| strrchr | दिए हुए string से एक character का आखिरी occurrence के आगे का string pointer को return करता है | |
| strrev | String को उलटी दिशा से print करता है | |
| strrstr | दिए हुए String का आखिरी string occurrence के आगे का string pointer को return करता है | |
| strset | दिए हुए character से पूरे string को replace करता है | |
| strstr | दिए हुए String का पहला string occurrence के आगे का string pointer को return करता है | |
| strupr | Lowercase के Characters को Uppercase में convert किया जाता है | |
strcat() - String Function
strcat ये String का एक Function है | एक String से दूसरे String को जोड़ा जाता है |Syntax for strcat()
strcat(destination_string, source_string);Destination_string - ये वो parameter है जिसके साथ source का string जोड़ा जाता है | String के आखिर में null character (\0) होता है | Source string destination string के साथ जुड़ते समय उसको remove कर देता है |
Source_string - ये वो parameter है जिसका string destination string के साथ आखिर में जोड़ा जाता है |
किसी integer value को एक variable को दूसरे variable से जोड़ना हो तो arithmetic operator (+) का use नहीं कर सकते |
for e.g.
int num1 = 5 ;
int num2 = 5 ;
int num3 = num1 + num2 ;
इनका output 55 मिलना चाहिए लेकिन इनका output 10 मिलेगा |
किसी char को भी एक दूसरे से arithmetic operators के साथ नहीं जोड़ा जा सकता |
Source Code :
#include <stdio.h>
#include <string.h>
int main (){
char str1[20] = "Welcome";
char str2[20] = " Friend";
strcat(str1, str2);
printf("Concatenation String : %s", str1);
return 0;
}
Output :
Concatenation String : Welcome Friend
strchr() - String Function
दिए हुए string से एक character का पहला occurrence के आगे का string pointer को return करता है |Syntax for strchr()
strchr(string, int character);string - ये एक normal string है |
int character - ये दिए हुए string में से दिए हुए character का पहला occurrence के आगे का string pointer को return करता है |
अगर दिया हुआ character string को नहीं मिलता तो वो NULL character return करता है |
Source Code :
#include <stdio.h>
#include <string.h>
int main (){
char str[] = "Hello Friends";
char *ptr;
ptr = strchr(str, 'F');
printf("%s", ptr);
return(0);
}
Output :
Friends
strcmp() - String Function
दो String को Compare किया जाता है | ये case-sensetive है |Syntax for strcmp()
strcmp(string1, string2);string1 -ये वो String है जिसके साथ String2 को Compare किया जाता है |
string2 - ये वो String है जिसके साथ String1 को Compare किया जाता है |
अगर दोनों string एक जैसे होते है तो ये '0' return करता है |अगर दोनों string अलग-अलग होते है तो '1' या '-1' return करता है |
Note : ये strcmp() function case-sensitive है | इसमें 'h' और 'H' ये दोनों अलग-अलग है |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str1[] = "Hello" ;
char str2[] = "World" ;
int a = strcmp(str1, str2) ;
printf ("%d\n", a) ;
int b = strcmp(str1, "Hello") ;
printf ("%d\n", b) ;
int c = strcmp(str1, "hello") ; // strcmp is case-sensitive
printf ("%d\n", c) ;
return 0;
}
Output :
-1 0 -1
strcmpi() - String Function
दो String को Compare किया जाता है | ये case-sensetive नहीं है |Syntax for strcmpi()
strcmpi(string1, string2);string1 - ये वो String है जिसके साथ String2 को Compare किया जाता है |
string2 - ये वो String है जिसके साथ String1 को Compare किया जाता है |
अगर दोनों string एक जैसे होते है तो ये '0' return करता है |अगर दोनों string अलग-अलग होते है तो '1' या '-1' return करता है |
Note : ये strcmpi() function case-sensitive है | इसमें 'h' और 'H' ये एक ही अलग-अलग है |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str1[] = "Hello" ;
char str2[] = "World" ;
int a = strcmpi(str1, str2) ;
printf ("%d\n", a) ;
int b = strcmpi(str1, "Hello") ;
printf ("%d\n", b) ;
int c = strcmpi(str1, "hello") ; // strcmpi is not case-sensitive
printf ("%d\n", c) ;
return 0;
}
Output :
-1 0 0
strcpy() - String Function
दो String को Compare किया जाता है | ये case-sensetive नहीं है |Syntax for strcpy()
strcpy(destination_string, source_string);destination_string - ये वो parameter है जिसपर source के string की value copy की जाती है | अगर destination string पर कोई value भी हो तो वो overwrite हो जाती है |
source_string - ये वो parameter है जिसकी value destination पर copy की जाती है |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str1[20] = "Welcome";
char str2[20] = "Friend";
printf("Value of str2 = %s\n", str2 );
strcpy(str2, str1);
printf("Copy str1 to str2 = %s", str2 );
return 0;
}
Output :
Value of str2 = Friend Copy str1 to str2 = Welcome
strdup() - String Function
String का duplicate बनाता है |Syntax for strdup()
strdup(string);string - ये String है जिसका duplicate बनाया जाता है |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str[] = "Hello World";
char *ptr;
ptr = strdup(str);
printf("Duplicate string : %s", ptr);
return 0;
}
Output :
Duplicate string : Hello World
strlen() - String Function
String की Length निकाली जाती है |Syntax for strlen()
strlen(string);string - ये एक normal string है, जिसकी length निकली जाती है |
strlen से निकला हुआ output integer value ही होती है ,ये किसी दूसरे integer variable में भी store करके रख सकते है |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str[30] = "Hello World";
int length ;
printf("String : %s\n", str);
length = strlen(str);
printf("Length of str is %d", length);
return 0;
}
Output :
String : Hello World Length of str is 11
strlwr() - String Function
Uppercase के Characters को Lowercase में convert किया जाता है |Syntax for strlwr()
strlwr(string);string - ये वो string है जिसको lowercase में convert किया जाता है |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str[50] = "HELLO WORLD";
int lwr ;
printf("String : %s\n", str );
printf("Lowercase of str = %s", strlwr(str));
return 0;
}
Output :
String : HELLO WORLD Lowercase of str = hello world
strncat() - String Function
दिए हुए number के जितने character है उनसे String को जोड़ा जाता है |Syntax for strncat()
strncat(destination_string, source_string, size_t num);destination_string - ये वो string जिसके साथ source string को जोड़ा जाता है |
source_string - ये वो string जिसके साथ destination string को बाद में जोड़ा जाता है |
size_t num - यहाँ पर जो integer value दी जाती है उतने character वो source string से लेता है |
Program में strncat(str1, str2, 2); ऐसा लिखा है , इसका मतलब ये है कि, ये 2 characters str2 से लेगा |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str1[20] = "Hello";
char str2[20] = " World";
strncat(str1, str2, 2);
printf("String : %s\n", str1);
return 0;
}
Output :
String : Hello W
strncpy() - String Function
दिए हुए number के जितने character एक string से दूसरे string में copy किया जाता है |Syntax for strncpy()
strncpy(destination_string, source_string, size_t num);destination_string - ये वो parameter है जिसपर source के string की value copy की जाती है | अगर destination string पर कोई value भी हो तो वो overwrite हो जाती है |
source_string - ये वो parameter है जिसकी value destination पर copy की जाती है |
size_t num - यहाँ पर जो integer value दी जाती है उतने character वो destination string से लेकर source string पर copy कर देता है |
Program में strncpy(str1, str2, 2); ऐसा लिखा है , इसका मतलब ये है कि, ये 2 characters str2 से लेगा |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str1[20] = "Welcome";
char str2[20] = "Friend";
printf("Value of str2 = %s\n", str2 );
strncpy(str2, str1, 2);
printf("Copy str1 to str2 = %s", str2 );
return 0;
}
Output :
Value of str2 = Friend Copy str1 to str2 = Weiend
strnset() - String Function
दिए हुए number और दिए हुए character के हिसाब से string को replace करता है |Syntax for strnset()
strnset(string, char ch, int c);destination_string - ये एक normal string है |
char ch - ये वो character है जिससे string के हर character को replace किया जाता है |
int c - यहाँ पर जितना number है उतने character string से replace किया जाते है |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str[20] = "Hello World";
printf("Value of str = %s\n", str);
strnset(str, '.', 2);
printf("%s", str);
return 0;
}
Output :
Value of str = Hello World ..llo World
strrchr() - String Function
दिए हुए string से एक character का आखिरी occurrence के आगे का string pointer को return करता है |Syntax for strrchr()
strrchr(string, int character);string - ये एक normal string है |
int character - ये वो character है जिसका आखिरी occurrence के आगे का string pointer को return किया जाता है |
अगर strrchr() function को कोई character नहीं मिलता तो वो NULL character return करता है |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str[20] = "Hello World";
char *ptr;
ptr = strrchr(str, 'o');
printf("%s", ptr);
return 0;
}
Output :
orld
strrev() - String Function
String को उलटी दिशा से print करता है |Syntax for strrev()
strrev(string);string - ये एक normal string है |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str[20] = "Hello World";
strrev(str);
printf("%s", str);
return 0;
}
Output :
dlroW olleH
strrstr() - String Function
दिए हुए String का आखिरी string occurrence के आगे का string pointer को return करता है |Syntax for strrstr()
strrstr(string1, string2);string1 - ये एक normal string है |
string2 - string1 में से ये string find करके उसका आखिरी occurrence pointer को return करता है |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str[100] = "Hi How are you? Hi I am Fine";
char *ptr;
ptr = strrstr(str, "Hi");
printf("%s", ptr);
return 0;
}
Output :
Hi I am Fine
strset() - String Function
दिए हुए String का आखिरी string occurrence के आगे का string pointer को return करता है |Syntax for strset()
strset(string, int character);string - ये एक normal string है |
int character - ये एक-एक character करके सभी characters को replace कर देता है |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str[50] = "Hi How are you? Hi I am Fine";
strset(str, '$');
printf("%s", str);
return 0;
}
Output :
$$$$$$$$$$$$$$$$$$$$$
strstr() - String Function
दिए हुए String का आखिरी string occurrence के आगे का string pointer को return करता है |Syntax for strstr()
strstr(string1, string2);string - ये एक normal string है |
int character - string1 में से ये string find करके उसका पहला occurrence pointer को return करता है |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str[100] = "Hi How are you? Hi I am Fine";
char *ptr;
ptr = strstr(str, "Hi");
printf("%s", ptr);
return 0;
}
Output :
Hi How are you? Hi I am Fine
strupr() - String Function
Lowercase के Characters को Uppercase में convert किया जाता है |Syntax for strupr()
strupr(string);string - ये वो string है जिसको Uppercase में convert किया जाता है |
Source Code :
#include <stdio.h>
#include <string.h>
int main(){
char str[50] = "hello world";
int lwr ;
printf("String : %s\n", str );
printf("Uppercase of str = %s", strupr(str));
return 0;
}
Output :
String : hello world Uppercase of str = HELLO WORLD







No comments: