Strings

Understanding Strings in C Programming

Introduction to Strings (स्ट्रिंग का परिचय)

Strings character arrays होते हैं जो null character ('\0') से terminate होते हैं। ये text data को represent करने के लिए use किए जाते हैं।

Strings are character arrays that are terminated by a null character ('\0'). They are used to represent text data.

Key Points (मुख्य बिंदु):

  • Strings null-terminated character arrays होते हैं
  • Strings are null-terminated character arrays
  • String length null character से पहले के characters की count होती है
  • String length is the count of characters before the null character
  • String functions null character को automatically handle करते हैं
  • String functions automatically handle the null character

String Declaration and Initialization (स्ट्रिंग डिक्लेरेशन और इनिशियलाइजेशन)

Strings को declare और initialize करने के different ways:

Different ways to declare and initialize strings:

// String declaration
char str1[20];

// String initialization
char str2[] = "Hello";
char str3[10] = "World";
char str4[] = {'H', 'e', 'l', 'l', 'o', '\0'};

// String input
char name[50];
printf("Enter your name: ");
scanf("%s", name);  // Reads until whitespace
fgets(name, 50, stdin);  // Reads entire line
'H'
'e'
'l'
'l'
'o'
'\0'

String Functions (स्ट्रिंग फंक्शन्स)

C में commonly used string functions:

Commonly used string functions in C:

#include 

// String length
char str[] = "Hello";
int len = strlen(str);  // Returns 5

// String copy
char src[] = "Source";
char dest[20];
strcpy(dest, src);  // Copies src to dest

// String concatenation
char str1[20] = "Hello";
char str2[] = " World";
strcat(str1, str2);  // str1 becomes "Hello World"

// String comparison
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);  // Returns negative if str1 < str2
Function (फंक्शन) Description (विवरण) Example (उदाहरण)
strlen() String की length return करता है strlen("Hello") = 5
strlen() Returns the length of string strlen("Hello") = 5
strcpy() एक string को दूसरी में copy करता है strcpy(dest, src)
strcpy() Copies one string to another strcpy(dest, src)

String Operations (स्ट्रिंग ऑपरेशन्स)

Strings पर perform किए जाने वाले common operations:

Common operations performed on strings:

// String traversal
char str[] = "Hello";
for (int i = 0; str[i] != '\0'; i++) {
    printf("%c", str[i]);
}

// String modification
char str[] = "Hello";
str[0] = 'J';  // Changes to "Jello"

// String searching
char str[] = "Hello World";
char *ptr = strchr(str, 'W');  // Returns pointer to 'W'

// String tokenization
char str[] = "Hello,World,How,Are,You";
char *token = strtok(str, ",");
while (token != NULL) {
    printf("%s\n", token);
    token = strtok(NULL, ",");
}
  • String Traversal: Loop का use करके string के characters को access करें
  • String Traversal: Access string characters using loops
  • String Modification: Individual characters को modify करें
  • String Modification: Modify individual characters
  • String Searching: strchr() और strstr() का use करें
  • String Searching: Use strchr() and strstr()
  • Best Practices (सर्वोत्तम प्रथाएं)

    Strings के साथ काम करने के best practices:

    Best practices for working with strings:

  • Buffer Size: String array का size sufficient होना चाहिए
  • Buffer Size: String array size should be sufficient
  • Null Termination: Strings को always null-terminate करें
  • Null Termination: Always null-terminate strings
  • Safe Functions: strncpy() और strncat() जैसे safe functions का use करें
  • Safe Functions: Use safe functions like strncpy() and strncat()
  • Input Validation: User input को always validate करें
  • Input Validation: Always validate user input