Wednesday, October 29, 2025

String and Char with function

 


 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:

#include <stdio.h> #include <ctype.h> int main() { char ch = 'A'; if(isalpha(ch)) printf("%c is an alphabet.\n", ch); else printf("%c is not an alphabet.\n", ch); return 0; }

Output:

A is an alphabet.

isdigit(ch)

Use: Checks whether the character is a digit (0–9).

Example:

#include <stdio.h> #include <ctype.h> int main() { char ch = '5'; if(isdigit(ch)) printf("%c is a digit.\n", ch); else printf("%c is not a digit.\n", ch); return 0; }

Output:

5 is a digit.

isalnum(ch)

Use: Checks whether the character is alphanumeric (either a letter or a digit).

Example:

#include <stdio.h> #include <ctype.h> int main() { char ch = 'A'; if(isalnum(ch)) printf("%c is alphanumeric.\n", ch); else printf("%c is not alphanumeric.\n", ch); return 0; }

Output:

A is alphanumeric.

isspace(ch)

Use: Checks if the character is a space, tab, or newline character.

Example:

#include <stdio.h> #include <ctype.h> int main() { char ch = ' '; if(isspace(ch)) printf("Character is a space.\n"); else printf("Character is not a space.\n"); return 0; }

Output:

Character is a space.

isupper(ch)

Use: Checks whether the character is uppercase (A–Z).

Example:

#include <stdio.h> #include <ctype.h> int main() { char ch = 'M'; if(isupper(ch)) printf("%c is uppercase.\n", ch); else printf("%c is not uppercase.\n", ch); return 0; }

Output:

M is uppercase.

islower(ch)

Use: Checks whether the character is lowercase (a–z).

Example:

#include <stdio.h> #include <ctype.h> int main() { char ch = 'a'; if(islower(ch)) printf("%c is lowercase.\n", ch); else printf("%c is not lowercase.\n", ch); return 0; }

Output:

a is lowercase.

toupper(ch)

Use: Converts a lowercase character to uppercase.

Example:

#include <stdio.h> #include <ctype.h> int main() { char ch = 'b'; printf("Uppercase of %c is %c\n", ch, toupper(ch)); return 0; }

Output:

Uppercase of b is B

tolower(ch)

Use: Converts an uppercase character to lowercase.

Example:

#include <stdio.h> #include <ctype.h> int main() { char ch = 'G'; printf("Lowercase of %c is %c\n", ch, tolower(ch)); return 0; }

Output:

Lowercase of G is g

 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)

#include <stdio.h> #include <string.h> int main() { char str[50], rev[50]; printf("Enter a string: "); gets(str); strcpy(rev, str); strrev(rev); if(strcmp(str, rev) == 0) printf("The string \"%s\" is a Palindrome.\n", str); else printf("The string \"%s\" is not a Palindrome.\n", str); return 0; }

 

No comments:

Post a Comment

String and Char with Function (In hindi)

  1. Character (अक्षर) अर्थ: एक Character (अक्षर) एक अकेला वर्ण, अंक या चिन्ह होता है जिसे single quotes (‘ ’) में लिखा जाता है। उदाहरण: ...