STRINGS IN C
In C, strings are arrays of characters that are terminated by a null character ('\0'). Strings are
a sequence of characters stored in memory. In C, there is no built-in string type as in other
languages, but strings can be managed using character arrays.
Declaring and Initializing Strings
char str1[] = "Hello";
char str2[6] = "World"; // Must include space for null character
char str3[100]; // Empty string, to be filled later
char str[] = "Hello”;
The array str in memory looks like this:
['H', 'e', 'l', 'l', 'o', '\0']
Declaration of Strings in C
A string in C is typically declared as a character array or as a pointer to char.
char str[20]; // An array of 20 characters
This array can store a string of up to 19 characters (since the last position must be reserved for
the null character '\0').
String Initialization:
You can initialize a string when declaring the array:
char str[] = "Hello"; // Automatically includes null terminator
COMPILED BY: ER.GAURAB MISHRA
HOD, COMPUTER DEPARTMENT
BAGBAZAR
String handling functions
String handling functions in C are available in the <string.h> library. These functions are
used to manipulate and process strings (character arrays). Here's a list of the most commonly
used string handling functions in C:
1. strlen()
Purpose: Returns the length of a string (excluding the null character \0).
2. strcpy()
Purpose: Copies one string to another.
3. strncpy()
Purpose: Copies the first n characters of one string to another.
4. strcat()
Purpose: Appends one string to the end of another.
5. strncat()
Purpose: Appends the first n characters of a string to another.
6. strcmp()
Purpose: Compares two strings lexicographically.
Return:
0 if both strings are equal
>0 if str1 > str2
<0 if str1 < str2
strlwr()
Description: Converts all uppercase characters in a string to lowercase.
strupr()
COMPILED BY: ER.GAURAB MISHRA
HOD, COMPUTER DEPARTMENT
BAGBAZAR
Description: Converts all lowercase characters in a string to uppercase.
strrev()
Description: Reverses the characters of a string in place.
1. strlen() – Find Length of a String
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello";
int length = strlen(str);
printf("Length of string = %d\n", length);
return 0;
}
2. strcpy() – Copy One String to Another
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello";
char destination[50];
strcpy(destination, source);
printf("Copied string = %s\n", destination);
return 0;
}
3. strcat() – Concatenate Two Strings
#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "Hello ";
char str2[] = "World";
strcat(str1, str2);
printf("Concatenated string = %s\n", str1);
return 0;
}
4. strcmp() – Compare Two Strings
#include <stdio.h>
COMPILED BY: ER.GAURAB MISHRA
HOD, COMPUTER DEPARTMENT
BAGBAZAR
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "Hello";
if (strcmp(str1, str2) == 0)
printf("Strings are equal\n");
else
printf("Strings are not equal\n");
return 0;
}
Character-by-character comparison:
Index str1[i] str2[i] Comparison Result
0 'a' 'a' 'a' == 'a' Continue
1 'p' 'p' 'p' == 'p' Continue
2 'p' 'r' 'p' < 'r' Stop here!
⚖️ Decision:
'p' has ASCII value 112
'r' has ASCII value 114
strcmp() returns: 112 - 114 = -2
🎯 Visualization:
str1: a p p l e
str2: a p r i c
o t
↑ ↑
Match until here
↑
First mismatch: 'p' < 'r'
📌 Final Verdict:
strcmp("apple", "apricot") returns a negative number because "apple" comes before
"apricot" in dictionary order.
COMPILED BY: ER.GAURAB MISHRA
HOD, COMPUTER DEPARTMENT
BAGBAZAR
5. strlwr() – Convert to Lowercase (Turbo C or MSVC only)
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "HELLO";
printf("Lowercase = %s\n", strlwr(str));
return 0;
}
6. strupr() – Convert to Uppercase (Turbo C or MSVC only)
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "hello";
printf("Uppercase = %s\n", strupr(str));
return 0;
}
7. strrev() – Reverse a String (Turbo C or MSVC only)
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello";
printf("Reversed string = %s\n", strrev(str));
return 0;
}
strncpy() – Copy a Specific Number of Characters from One String to Another
#include <stdio.h>
#include <string.h>
int main() {
char source[] = "Hello, World!";
char destination[20];
// Copy first 5 characters from source to destination
strncpy(destination, source, 5);
COMPILED BY: ER.GAURAB MISHRA
HOD, COMPUTER DEPARTMENT
BAGBAZAR
// Null-terminate the string explicitly
destination[5] = '\0';
printf("Copied string = %s\n", destination);
return 0;
}
strncat() – Concatenate a Specific Number of Characters to a String
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";
// Concatenate first 5 characters from str2 to str1
strncat(str1, str2, 5);
printf("Concatenated string = %s\n", str1);
return 0;
}
Tabular comparison between Formatted I/O and Unformatted I/O functions in C
Read or write data in a specific format using Read or write raw data without any
Definition
format specifiers (like %d, %s, etc.). format or conversion.
Example printf(), scanf(), sprintf(), fread(), fwrite(), getchar(),
Function fscanf() putchar()
Format Specifiers Uses format specifiers (e.g., %d, %f, %s). Does not use format specifiers.
Converts data types (e.g., integer to string, Does not convert data types, works with
Conversion
float to decimal). raw data exactly.
Speed Slower (due to formatting and conversions). Faster (no formatting or conversions).
Control Over Provides control over how data is displayed No control over formatting; data
Output (e.g., number of decimal places, alignment). is written exactly as it is.
Use When displaying output in a readable When handling binary data or working
Case format or getting formatted input. with files without formatting.
COMPILED BY: ER.GAURAB MISHRA
HOD, COMPUTER DEPARTMENT
BAGBAZAR
. getchar() - Input Function
Definition: Reads a single character from the standard input (keyboard)
#include <stdio.h>
int main() {
char ch;
// Read a single character from the user
ch = getchar();
printf("You entered: %c\n", ch);
return 0;
}
putchar() - Output Function
Definition: Writes a single character to the standard output (screen).
#include <stdio.h>
int main() {
char ch = 'A';
// Output a single character
putchar(ch);
return 0;
}
getch() - Input Function (Console)
Definition: Reads a single character from the console (keyboard) without waiting for the
user to press Enter.
#include <conio.h> // For MSVC and Turbo C++ only
int main() {
char ch;
// Read a single character without pressing Enter
ch = getch();
printf("You pressed: %c\n", ch);
return 0;
}
COMPILED BY: ER.GAURAB MISHRA
HOD, COMPUTER DEPARTMENT
BAGBAZAR
putch() - Output Function (Console)
Definition: Writes a single character to the console (screen) without a newline.
#include <conio.h> // For MSVC and Turbo C++ only
int main() {
char ch = 'B';
// Output a single character to the console
putch(ch);
return 0;
}
getche() Function in C
Definition: The getche() function is used to read a single character from the
keyboard. It is similar to getch(), but the key difference is that it echoes the character
back to the console as it is typed (i.e., the character is displayed on the screen as the user
presses the keys).
#include <conio.h> // For MSVC and Turbo C++ only
#include <stdio.h>
int main() {
char ch;
printf("Press any key: ");
// Reads a single character from the console and echoes it back
ch = getche();
printf("\nYou pressed: %c\n", ch);
return 0;
COMPILED BY: ER.GAURAB MISHRA
HOD, COMPUTER DEPARTMENT
BAGBAZAR
gets() Function
Definition: gets() is used to read a string of characters from the standard input
(keyboard) until a newline character (\n) is encountered. It then terminates the string with
a null character (\0).
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
// Read a string from the user
gets(str);
printf("You entered: %s\n", str);
return 0;
}
puts() Function
Definition: puts() is used to write a string to the standard output (screen), followed by
a newline character (\n).
#include <stdio.h>
int main() {
char str[] = "Hello, world!";
// Write the string to the screen
puts(str); // puts() automatically appends a newline (\n) after the string.
return 0;
}
COMPILED BY: ER.GAURAB MISHRA
HOD, COMPUTER DEPARTMENT
BAGBAZAR