ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCC-112Characters Strings Lib…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CC-112

    Programming Fundamentals

    Progress0 / 39 done
    Topics39
    1Introduction to Problem Solving, Algorithms, Programming, and C Language2Problem Solving, a brief review of Von-Neumann Architecture3The C Programming Language, Pseudo-code, Concept of Variable4Data types in Pseudo-code, The C Standard Library and Open Source5Input/Output, Arithmetic expressions, Assignment statement, Operator precedence6Concept of Integer division, Flowchart and its notations7Typical C Program Development Environment, Role of Compiler and Linker8Test Driving C Application
    9A Simple C Program: Printing Text, Adding Two Integer10Memory Concepts, Arithmetic in C, Operators
    11Equality and Relational Operators
    12The if, if...else, while Nested Control Statements
    13for, switch, do...while, break, continue, Logical Operators
    14Modularizing Program in C, Math Library Functions15Function Definitions and Prototypes, Function-Call Stack and Stack Frames16Stack rolling and unrolling, Headers, Passing Arguments by Value and by Reference17Random Number Generation, Scope Rules, Recursion, Recursion vs Iteration
    18Defining Arrays, Character Arrays, Static and Automatic Local Arrays19Passing Arrays to Function, Sorting and Searching Arrays20Multidimensional and Variable Length Arrays
    21Pointer Definitions and Initialization, Pointer Operators22Passing Arguments to Function by Reference, Using the const and sizeof Operator23Pointer Expressions and Arithmetic, Pointers and Arrays, Array of Pointers24Function Pointers
    25Strings and Characters, Character Handling Library26String Functions, Library Functions
    27Streams, Formatted Output with printf, Formatted Input with scanf
    28Defining Structures, Accessing Structure Member, Structures and Functions29typedef, Unions
    30Bitwise Operators, Bit Fields, Enumeration Constants
    31Files and Streams, Creating, Reading and Writing data to a Sequential and a Random-Access File
    32#include, #define, Conditional Compilation, #error and #pragma33# and ## Operators, Predefined Symbolic Constants, Assertions
    34Variable Length Argument List, Using Command Line Arguments35Compiling Multiple-Source-File Programs, Program Termination with exit and atexit36Suffixes for Integer and Floating-Point Literals, Signal Handling
    37calloc and realloc, goto
    38Self-Referential Structures, Linked Lists39Efficiency of Algorithms, Selection and Insertion Sort
    7 min read
    Topic 25 of 39
    Topic Study Note Illustration
    25

    Characters and Strings: Strings and Characters, Character Handling Library

    In C programming, characters and strings are fundamental data types used to represent textual data. Understanding how to work with characters and strings, and using the character...

    Characters and Strings in C: Strings and Characters, Character Handling Library

    In C programming, characters and strings are fundamental data types used to represent textual data. Understanding how to work with characters and strings, and using the character handling library, is essential for text manipulation tasks in C programs.

    Let's break down these concepts in detail:


    1. Strings and Characters in C

    Characters

    • A character in C is a single unit of data representing a letter, number, symbol, or whitespace. It is stored as an integer value (ASCII or Unicode representation) in memory.
    • In C, a character is enclosed in single quotes ('a', '1', '%').
    • The C programming language uses the ASCII standard to represent characters, where each character is mapped to an integer value. For example:
      • 'A' is represented by the ASCII value 65
      • 'a' is represented by 97
      • '1' is represented by 49

    A character is typically declared as char, which is a data type used to store a single character.

    char letter = 'A';  // A single character
    

    Strings

    • A string in C is an array of characters terminated by a null character ('\0'), which indicates the end of the string.
    • Strings are enclosed in double quotes ("Hello", "1234", "Welcome to C!").
    char str[] = "Hello, World!";  // A string
    

    In memory, the string Hello, World! is represented as:

    'H' 'e' 'l' 'l' 'o' ',' ' ' 'W' 'o' 'r' 'l' 'd' '!' '\0'
    

    The null character '\0' ensures that functions can determine where the string ends.


    2. String Handling in C

    In C, strings are essentially arrays of characters. There is no special data type for strings, and string manipulation is done using arrays and various library functions. C does not automatically manage the size of strings, so programmers must ensure that strings are properly terminated with a null character.

    Example of String Declaration and Initialization

    #include <stdio.h>
    
    int main() {
        char name[] = "John Doe";  // String initialization
    
        printf("Name: %s\n", name);  // Print the string
    
        return 0;
    }
    

    Here:

    • char name[] = "John Doe"; initializes a string. The compiler implicitly adds the null character '\0' at the end of the string.
    • %s is the format specifier used in printf to print strings.

    Output:

    Name: John Doe
    

    3. Character Handling Library

    C provides a set of functions for handling characters and strings, which are included in the <ctype.h> and <string.h> libraries. These functions are essential for tasks like comparing strings, copying strings, converting characters, and more.


    Functions in <ctype.h>: Character Handling

    The <ctype.h> library provides functions that allow you to classify and manipulate characters. Some of the most commonly used functions include:

    1. isalpha()

    Checks if a character is an alphabetic letter (either lowercase or uppercase).

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

    2. isdigit()

    Checks if a character is a digit (0-9).

    #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;
    }
    

    3. islower() and isupper()

    Check whether a character is lowercase or uppercase.

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

    4. tolower() and toupper()

    Convert characters to lowercase or uppercase.

    #include <stdio.h>
    #include <ctype.h>
    
    int main() {
        char ch = 'a';
    
        // Convert to uppercase
        printf("Uppercase: %c\n", toupper(ch));
    
        // Convert to lowercase
        printf("Lowercase: %c\n", tolower(ch));
    
        return 0;
    }
    

    5. isalpha() and isalnum()

    • isalpha(): Checks if a character is alphabetic.
    • isalnum(): Checks if a character is alphanumeric (i.e., a letter or a digit).
    #include <stdio.h>
    #include <ctype.h>
    
    int main() {
        char ch = '8';
    
        if (isalnum(ch)) {
            printf("%c is alphanumeric.\n", ch);
        } else {
            printf("%c is not alphanumeric.\n", ch);
        }
    
        return 0;
    }
    

    Functions in <string.h>: String Handling

    The <string.h> library provides functions for manipulating strings. These include functions for copying, concatenating, comparing, and searching strings.

    1. strlen()

    Returns the length of the string (not including the null terminator).

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char str[] = "Hello, World!";
        
        printf("Length of string: %lu\n", strlen(str));  // 13 (not counting the null terminator)
        
        return 0;
    }
    

    2. strcpy()

    Copies a string to another string.

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char source[] = "Hello";
        char destination[20];
        
        strcpy(destination, source);  // Copying source to destination
        
        printf("Copied string: %s\n", destination);
        
        return 0;
    }
    

    3. strcmp()

    Compares two strings lexicographically (returns 0 if equal, a negative value if the first string is smaller, and a positive value if the first string is greater).

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char str1[] = "apple";
        char str2[] = "banana";
        
        int result = strcmp(str1, str2);
        
        if (result < 0) {
            printf("'%s' is less than '%s'.\n", str1, str2);
        } else if (result > 0) {
            printf("'%s' is greater than '%s'.\n", str1, str2);
        } else {
            printf("'%s' is equal to '%s'.\n", str1, str2);
        }
    
        return 0;
    }
    

    4. strcat()

    Concatenates two strings (appends one string to the end of another).

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char str1[20] = "Hello";
        char str2[] = " World!";
        
        strcat(str1, str2);  // Concatenates str2 to str1
        
        printf("Concatenated string: %s\n", str1);
        
        return 0;
    }
    

    5. strchr()

    Finds the first occurrence of a character in a string.

    #include <stdio.h>
    #include <string.h>
    
    int main() {
        char str[] = "Hello, World!";
        char *ptr = strchr(str, 'W');
        
        if (ptr != NULL) {
            printf("Character found at position: %ld\n", ptr - str);
        } else {
            printf("Character not found.\n");
        }
    
        return 0;
    }
    

    4. Summary of Key Concepts

    • Characters are single units of text, represented by char in C. They are typically stored as integers (ASCII values).

    • Strings are arrays of characters that are terminated by a null character ('\0'). They are handled using arrays and pointers in C.

    • The <ctype.h> library provides functions for character classification and manipulation, such as isalpha(), isdigit(), toupper(), tolower(), etc.

    • The <string.h> library provides functions for working with strings, such as strlen(), strcpy(), strcat(), strcmp(), and more.

    Understanding how to manipulate characters and strings is essential for handling textual data in C programs, and the standard libraries provide a robust set of functions to aid in this process.

    ← Previous topic 24
    Function Pointers
    Next topic 26 →
    String Functions, Library Functions

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    7mremaining
    1.2kwords
    Intermediatelevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Programming Fundamentals