C Strings & String Functions
In C, a string is an array of characters terminated by a null character '\0'
. Strings are used to store text, and the <string.h> library provides many functions to manipulate them.
C में string characters का एक array होता है, जो '\0'
null character से समाप्त होता है। Strings को संभालने के लिए <string.h> लाइब्रेरी में कई functions उपलब्ध हैं।
String Declaration and Initialization
You can declare and initialize strings in different ways.
आप strings को अलग-अलग तरीकों से declare और initialize कर सकते हैं।
char str1[20] = "Hello";
char str2[] = "World";
char str3[20]; // Empty string for later input
String Input and Output
This program takes a string input from the user and prints it.
यह प्रोग्राम user से string input लेकर उसे print करता है।
#include <stdio.h>
int main() {
char name[50];
printf("Enter your name: ");
fgets(name, sizeof(name), stdin);
printf("Hello %s", name);
return 0;
}
strlen() – String Length
strlen(str)
returns the number of characters in a string (excluding the null terminator).
strlen(str)
string में characters की संख्या लौटाता है (null terminator को छोड़कर)।
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello";
printf("Length: %zu", strlen(str));
return 0;
}
strcpy() – Copy a String
strcpy(dest, src)
copies the content of one string into another.
strcpy(dest, src)
एक string की सामग्री को दूसरी string में कॉपी करता है।
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "World";
char dest[20];
strcpy(dest, src);
printf("Copied String: %s", dest);
return 0;
}
strcat() – Concatenate Strings
strcat(dest, src)
appends one string to the end of another.
strcat(dest, src)
एक string को दूसरी string के अंत में जोड़ता है।
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello ";
char str2[] = "World";
strcat(str1, str2);
printf("Concatenated String: %s", str1);
return 0;
}
strcmp() – Compare Strings
strcmp(str1, str2)
compares two strings and returns 0 if they are equal.
strcmp(str1, str2)
दो strings की तुलना करता है और अगर वे समान हैं तो 0 लौटाता है।
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
if (strcmp(str1, str2) == 0)
printf("Strings are equal");
else
printf("Strings are different");
return 0;
}