Strings in C
A string in C is a sequence of characters stored in a character array and terminated with the special character '\0' called the null terminator.
Unlike other languages, C does not have a built-in string datatypeβstrings are handled using arrays of char.
What is a String?
A string is a group of characters ending with a null character '\0'. This null terminator tells the compiler where the string ends.
- Stored inside a character array
- Last character is always '\0'
- Stored in consecutive memory locations
- Useful for storing names, words, messages, etc.
Example β Simple String
#include <stdio.h>
int main() {
char name[] = "Sourav";
printf("Name: %s", name);
return 0;
}
How Strings Are Stored in Memory
Each character uses 1 byte. After the last character, C adds '\0' automatically.
char s[] = "CAT"; /* Memory Layout: C | A | T | \0 */
Example β Display Characters Individually
#include <stdio.h>
int main() {
char s[] = "CAT";
for(int i = 0; i < 4; i++){
printf("%c\n", s[i]);
}
return 0;
}
Ways to Declare Strings
1. Declaring Without Initialization
char name[20];
2. Declaring With Initialization
char name[20] = "Sourav";
3. Character-by-Character
char word[4] = {'C','A','T','\0'};
Example β Taking Input into a String
#include <stdio.h>
int main() {
char country[30];
printf("Enter your country: ");
scanf("%s", country);
printf("Country: %s", country);
return 0;
}
String Input Methods
- scanf("%s") β stops at space
- gets() β reads full line (unsafe)
- fgets() β safest, reads spaces
Example β fgets() Input
#include <stdio.h>
int main() {
char sentence[50];
printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin);
printf("You wrote: %s", sentence);
return 0;
}
Important String Functions (string.h)
- strlen() β length of string
- strcpy() β copy string
- strcat() β join strings
- strcmp() β compare strings
- strupr() β convert to uppercase
- strlwr() β convert to lowercase
- strrev() β reverse string
β Some functions like strupr(), strlwr(), strrev() are not standard C functions,
but supported in many compilers (Turbo C/C++, GCC on Windows etc.).
Example β Using strlen()
#include <stdio.h>
#include <string.h>
int main() {
char city[] = "Bhubaneswar";
printf("Length = %lu", strlen(city));
return 0;
}
Example β Using strcpy()
#include <stdio.h>
#include <string.h>
int main() {
char a[] = "Hello";
char b[20];
strcpy(b, a);
printf("Copied: %s", b);
return 0;
}
Example β Using strcmp()
#include <stdio.h>
#include <string.h>
int main() {
char a[] = "Apple";
char b[] = "Mango";
if(strcmp(a, b) == 0)
printf("Same");
else
printf("Different");
return 0;
}
String Handling Rules
- Always ensure space for '\0'
- Use fgets() instead of gets()
- Always include <string.h>
- Character access must stay within array size
- Comparison must be done using strcmp()
Example β Uppercase Using Loop
#include <stdio.h>
int main() {
char s[] = "sourav";
for(int i = 0; s[i] != '\0'; i++){
if(s[i] >= 'a' && s[i] <= 'z')
s[i] -= 32;
}
printf("Uppercase: %s", s);
return 0;
}
Real Life Uses of Strings
- User credentials (name, username, password)
- Email IDs, mobile numbers
- Chat messages
- Website URLs
- File names
- Command line inputs
Practice Questions
- What is a string? How does C terminate a string?
- Explain memory layout of the string βHELLOβ.
- Difference between scanf("%s") and fgets().
- List 5 string functions with small descriptions.
- Why is gets() unsafe?
Practice Task
Write a program that takes a full name and prints:
β Total characters
β Vowels count
β Consonants count
β Reversed string
β Name in Title Case