Strings in C Language | Full Guide in Telugu
Part-9: C lo peru ela store cheyali? - LearnTechSoft
💡 Real Life: C lo int ante numbers ke, char ante okka letter ke. Mari "Ramesh" lanti peru ela? Adhe String! Chala characters kalipi okate String.
1. String ante enti?
String = Character Array + \0 (Null Character) - Prati string chivarlo \0 automatic ga untundi - Adhe string end ayyindi ani C ki telsedi.
R
A
M
E
S
\0
Index: 0 | 1 | 2 | 3 | 4 | 5 (Null)
Index: 0 | 1 | 2 | 3 | 4 | 5 (Null)
2. String Declaration
// Method 1: Best Method
char name[] = "Ramesh";
// Method 2: Size tho
char name[20] = "Ramesh";
// Method 3: Character by character
char name[7] = {'R','a','m','e','s','h','\0'};
// Print:
printf("%s", name); // %s for String
3. String Input - Important Difference!
⚠️ 2 Methods:
scanf("%s"): Oka word ke - "Ramesh Kumar" lo "Ramesh" matrame teesukuntundi (Space vaste aagipotundi)
gets() / fgets(): Full line - "Ramesh Kumar" motham teesukuntundi
scanf("%s"): Oka word ke - "Ramesh Kumar" lo "Ramesh" matrame teesukuntundi (Space vaste aagipotundi)
gets() / fgets(): Full line - "Ramesh Kumar" motham teesukuntundi
char fullName[50];
// For full name with space:
printf("Enter Full Name: ");
fgets(fullName, 50, stdin); // Best & Safe
printf("Hello %s", fullName);
4. Important String Functions - Library
Include cheyali: #include <string.h>
| Function | Panienti? | Example |
|---|---|---|
| strlen() | Length kanukkodaniki | strlen("Ramesh") = 6 |
| strcpy() | Copy cheyadaniki | strcpy(a, b) |
| strcat() | Jodinchadaniki | "Ram"+"esh" = "Ramesh" |
| strcmp() | Compare cheyadaniki | Equal ayithe 0 vastundi |
#include <stdio.h>
#include <string.h>
int main() {
char name1[20] = "Ramesh";
char name2[20] = " Kumar";
printf("Length: %d\n", strlen(name1)); // 6
strcat(name1, name2); // Jodinchi
printf("Full Name: %s\n", name1); // Ramesh Kumar
return 0;
}
🎯 Next Post: Pointers in C - Most Difficult but Important - Part-10
Practice: Name reverse, Palindrome check, Length without strlen()