1. Character
A character
is a single alphabet, digit, or symbol enclosed in single quotes (' ').
char ch = 'A';
Example:
char grade = 'B';
char digit = '9';
char symbol = '#';
Memory:
Each
character uses 1 byte in memory.
Character Handling Functions in C
All these functions are declared in the <ctype.h> header file.
They are used to check or convert characters.
isalpha(ch)
Use: Checks whether the character is an alphabet (A–Z or a–z).
Returns:
-
True (non-zero) if the character is an alphabet
-
False (0) otherwise
Example:
Output:
isdigit(ch)
Use: Checks whether the character is a digit (0–9).
Example:
Output:
isalnum(ch)
Use: Checks whether the character is alphanumeric (either a letter or a digit).
Example:
Output:
isspace(ch)
Use: Checks if the character is a space, tab, or newline character.
Example:
Output:
isupper(ch)
Use: Checks whether the character is uppercase (A–Z).
Example:
Output:
islower(ch)
Use: Checks whether the character is lowercase (a–z).
Example:
Output:
toupper(ch)
Use: Converts a lowercase character to uppercase.
Example:
Output:
tolower(ch)
Use: Converts an uppercase character to lowercase.
Example:
Output:
2. Character
Functions (from <ctype.h>)
|
Function |
Meaning |
Example |
Result |
|
isalpha(ch) |
Checks if
ch is an alphabet |
isalpha('A') |
True |
|
isdigit(ch) |
Checks if
ch is a digit |
isdigit('5') |
True |
|
isalnum(ch) |
Checks if
ch is alphabet or digit |
isalnum('A') |
True |
|
isspace(ch) |
Checks if
ch is space/tab |
isspace(' ') |
True |
|
isupper(ch) |
Checks if
ch is uppercase |
isupper('M') |
True |
|
islower(ch) |
Checks if
ch is lowercase |
islower('a') |
True |
|
toupper(ch) |
Converts
to uppercase |
toupper('b') |
'B' |
|
tolower(ch) |
Converts
to lowercase |
tolower('G') |
'g' |
3. Character
Array (String)
Definition:
A character
array is a collection of characters stored in continuous memory.
When it ends with \0 (null character), it becomes a string.
char name[10] = "Ravi";
Internally
stored as:
{'R', 'a', 'v', 'i', '\0'}
Declaration:
char name[20]; // only declared
char city[20] = "Balrampur"; // declared and initialized
4. Input / Output
Functions for Strings
|
Function |
Description |
Example |
|
scanf("%s", str); |
Takes
input (stops at space) |
HelloWorld |
|
fgets(str, size, stdin); |
Takes
input with spaces (safe) |
"Hello World" |
|
printf("%s", str); |
Prints
string |
|
|
puts(str); |
Prints
string with newline |
Example:
#include <stdio.h>
int main() {
char name[30];
printf("Enter
your name: ");
fgets(name, sizeof(name),
stdin);
printf("Hello
%s", name);
return 0;
}
5. String
Functions (from <string.h>)
|
Function |
Meaning |
Example |
Output |
|
strlen(str) |
Finds
length of string |
strlen("Hello") |
5 |
|
strcpy(dest, src) |
Copies one
string to another |
strcpy(a, b) |
a = b |
|
strcat(dest, src) |
Joins two
strings |
"Good" +
"Morning" |
"GoodMorning" |
|
strcmp(str1, str2) |
Compares
two strings |
"abc", "abc" |
0 (equal) |
|
strrev(str) |
Reverses
string (non-standard) |
"abc" |
"cba" |
|
strupr(str) |
Converts
to uppercase (non-standard) |
"hi" |
"HI" |
|
strlwr(str) |
Converts
to lowercase (non-standard) |
"HI" |
"hi" |
Example:
#include <stdio.h>
#include <string.h>
int main() {
char s1[20]
= "Hello";
char s2[20]
= "World";
printf("Length:
%lu\n", strlen(s1));
strcat(s1,
s2);
printf("Concat:
%s\n", s1);
strcpy(s2, "C
Language");
printf("Copy:
%s\n", s2);
printf("Compare:
%d\n", strcmp("abc", "abd"));
return 0;
}
Output:
Length: 5
Concat: HelloWorld
Copy: C Language
Compare: -1
String Manipulation Functions
in C
1) String Manipulation Functions are predefined functions in C language (available in
the header file <string.h>) that are used to perform various operations on strings,
such as:
finding length, copying, joining, comparing, changing case, or reversing
strings.
1. strlen() – String Length
Syntax:
int strlen(string);
Use:
Finds the total number of characters in a string (excluding '\0').
Example:
char name[] = "India";
printf("%d", strlen(name)); // Output: 5
2. strcpy() –
Copy String
Syntax:
strcpy(destination, source);
Use:
Copies one string into another.
Example:
char str1[20] = "Hello";
char str2[20];
strcpy(str2, str1);
printf("%s", str2); // Output: Hello
3. strcat() – Concatenate (Join) Strings
Syntax:
strcat(string1, string2);
Use:
Joins string2 at the end of string1.
Example:
char s1[20] = "Good ";
char s2[20] = "Morning";
strcat(s1, s2);
printf("%s", s1); // Output: Good Morning
4. strcmp() – Compare Two Strings
Syntax:
strcmp(string1, string2);
Use:
Compares two strings:
- Returns 0 if both are
equal
- Returns positive if
string1 > string2
- Returns negative if
string1 < string2
Example:
char a[] = "apple";
char b[] = "apple";
if(strcmp(a, b) == 0)
printf("Both
strings are same");
else
printf("Different
strings");
5. strrev() – Reverse String
Syntax:
strrev(string);
Use:
Reverses the given string.
Example:
char word[10] = "C";
strrev(word);
printf("%s", word);
6. strupr() – Convert to Uppercase
Syntax:
strupr(string);
Use:
Converts all characters to uppercase.
Example:
char name[20] = "india";
strupr(name);
printf("%s", name); // Output: INDIA
7. strlwr() – Convert to Lowercase
Syntax:
strlwr(string);
Use:
Converts all characters to lowercase.
Example:
char city[20] = "DELHI";
strlwr(city);
printf("%s", city); // Output: delhi
Complete Program Example
Program showing use of all string
manipulation functions.
#include <stdio.h>
#include <string.h>
int main() {
char str1[50]
= "Hello";
char str2[50]
= "World";
char str3[50];
printf("Original
Strings:\n");
printf("str1
= %s\n", str1);
printf("str2
= %s\n\n", str2);
// 1. Length
printf("Length
of str1 = %lu\n", strlen(str1));
// 2. Copy
strcpy(str3,
str1);
printf("After
strcpy, str3 = %s\n", str3);
// 3.
Concatenate
strcat(str1,
str2);
printf("After
strcat, str1 = %s\n", str1);
// 4.
Compare
if(strcmp(str2,
str3) == 0)
printf("str2
and str3 are same\n");
else
printf("str2
and str3 are different\n");
// 5. Reverse
strrev(str1);
printf("Reversed
str1 = %s\n", str1);
// 6.
Uppercase and Lowercase
strupr(str2);
strlwr(str3);
printf("Uppercase
str2 = %s\n", str2);
printf("Lowercase
str3 = %s\n", str3);
return 0;
}
Output:
Original Strings:
str1 = Hello
str2 = World
Length of str1 = 5
After strcpy, str3 = Hello
After strcat, str1 = HelloWorld
str2 and str3 are different
Reversed str1 = dlroWolleH
Uppercase str2 = WORLD
Lowercase str3 = hello
Summary Table
|
Function |
Meaning |
Header |
Example |
|
strlen() |
Length of
string |
<string.h> |
strlen("Hello") |
|
strcpy() |
Copy
string |
<string.h> |
strcpy(b,a) |
|
strcat() |
Join
strings |
<string.h> |
strcat(a,b) |
|
strcmp() |
Compare
strings |
<string.h> |
strcmp(a,b) |
|
strrev() |
Reverse
string |
<string.h> |
strrev(a) |
|
strupr() |
Convert to
upper |
<string.h> |
strupr(a) |
|
strlwr() |
Convert to
lower |
<string.h> |
strlwr(a) |
Functions that are Non-Standard- A function that is not defined in the official C standard, but is added by some specific compiler
(like Turbo C) for extra convenience.
|
Function |
Description |
Status |
|
strrev(str) |
Reverses
string |
❌
Non-standard |
|
strupr(str) |
Converts
to uppercase |
❌
Non-standard |
|
strlwr(str) |
Converts
to lowercase |
❌
Non-standard |
6.
Difference Between Character and String
|
Feature |
Character |
String |
|
Definition |
Single
symbol |
Group of
characters |
|
Quotes |
Single
quotes 'A' |
Double
quotes "A" |
|
Size |
1 byte |
Many bytes
+ \0 |
|
Library |
<ctype.h> |
<string.h> |
|
End
Character |
None |
Must end
with '\0' |
Palindrome Program (Using String Functions)
No comments:
Post a Comment