Arrays and strings in C language

Introduction

C language में strings को store करने के लिए अलग से कोई built-in data type नहीं होता।
इसलिए strings को store और process करने के लिए character array (char array) का उपयोग किया जाता है।

मतलब:

  • String = characters का collection
  • और यह array के रूप में store होता है

उदाहरण:

  • “HELLO” → यह actually एक char array है

Strings programming में बहुत important हैं क्योंकि:

  • name store करना
  • messages display करना
  • text processing करना

Exam में string handling programs बहुत important होते हैं।

Definition

String एक character array होता है जो characters की sequence को store करता है और जिसका अंत null character (\0) से होता है।

String Declaration

Syntax

char str[size];

Example

char name[10];

String Initialization

char str[] = "HELLO";

Actual storage:

H E L L O \0

\0 → null character (string end mark करता है)

Example 1: String Input and Output

#include <stdio.h>
int main()
{
char name[20];

printf("Enter name: ");
scanf("%s", name);

printf("Name: %s", name);
return 0;
}

Explanation:

  • %s string input/output के लिए use होता है
  • scanf space तक input लेता है

Example 2: String Length (without library)

#include <stdio.h>
int main()
{
char str[] = "HELLO";
int i = 0;

while (str[i] != '\0')
{
i++;
}
printf("Length = %d", i);

return 0;
}

Output:

Length = 5

Explanation:

  • loop तब तक चलता है जब तक \0 नहीं मिलता

Example 3: String Reverse

#include <stdio.h>
int main()
{
char str[] = "ABC";
int i = 0, len = 0;

while (str[i] != '\0')
{
len++;
i++;
}
for (i = len - 1; i >= 0; i--)
{
printf("%c", str[i]);
} return 0;
}

Output:

CBA

Explanation:

  • पहले length निकाली
  • फिर reverse order में print किया

Example 4: String Copy

#include <stdio.h>
int main()
{
char str1[] = "HELLO";
char str2[10];
int i = 0;

while (str1[i] != '\0')
{
str2[i] = str1[i];
i++;
}
str2[i] = '\0';

printf("%s", str2);
return 0;
}

Output:

HELLO

Explanation:

  • एक-एक character copy किया गया
  • अंत में null character add किया

Example 5: String Compare

#include <stdio.h>
int main()
{
char str1[] = "ABC";
char str2[] = "ABC";
int i = 0, flag = 1;

while (str1[i] != '\0' || str2[i] != '\0')
{
if (str1[i] != str2[i])
{
flag = 0;
break;
}
i++;
}

if (flag == 1)
printf("Equal");
else
printf("Not Equal");

return 0;
}

Output:

Equal

Important String Functions (Library)

FunctionUse
strlen()length निकालना
strcpy()copy करना
strcmp()compare करना
strcat()concatenate करना

Technical Understanding

  • String = char array + null character
  • \0 बहुत important है
  • loops से string processing होती है
  • array indexing से characters access होते हैं

Important Points

  • String हमेशा \0 से end होती है
  • %s string input/output के लिए use होता है
  • scanf space तक input लेता है
  • character by character processing होती है

Exam Points

  • String = character array
  • Null character (\0) बहुत important है
  • String handling programs frequently आते हैं
  • Reverse, copy, compare बहुत important programs हैं
  • Library functions भी पूछे जाते हैं

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top