📘 Lesson · Lesson 67
String Without string.h
String Without string.h
Working Without string.h
💡 Note
You can find string length, copy and reverse manually without library functions like strlen or strcpy.
String Length
C
#include <stdio.h>
int main() {
char str[] = "Hello";
int len = 0;
while (str[len] != '\0') len++; // count until null
printf("Length: %d", len); // 5
return 0;
}Output:
Length: 5
Length: 5
Reverse
C
char str[] = "abc", rev[10];
int len = 0;
while (str[len]) len++;
for (int i = 0; i < len; i++)
rev[i] = str[len-1-i];
rev[len] = '\0';
printf("%s", rev); // cbaOutput:
cba
cba
Summary
- A C string ends with the null character
\0— loop until you reach it. - Reverse by copying characters from the end to a new array.
string.h के बिना काम
💡 Note
आप string की length, copy और reverse manually कर सकते हैं बिना strlen या strcpy जैसी library functions के।
String Length
C
#include <stdio.h>
int main() {
char str[] = "Hello";
int len = 0;
while (str[len] != '\0') len++; // null तक count
printf("Length: %d", len); // 5
return 0;
}Output:
Length: 5
Length: 5
Reverse
C
char str[] = "abc", rev[10];
int len = 0;
while (str[len]) len++;
for (int i = 0; i < len; i++)
rev[i] = str[len-1-i];
rev[len] = '\0';
printf("%s", rev); // cbaOutput:
cba
cba
सारांश
- C string null character
\0पर खत्म होती है — उस तक loop करें। - Reverse को characters अंत से नई array में copy करें।