ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Programming Fundamentals
    CC-112
    Progress0 / 39 topics
    Topics
    1. Introduction to Problem Solving, Algorithms, Programming, and C Language2. Problem Solving, a brief review of Von-Neumann Architecture3. The C Programming Language, Pseudo-code, Concept of Variable4. Data types in Pseudo-code, The C Standard Library and Open Source5. Input/Output, Arithmetic expressions, Assignment statement, Operator precedence6. Concept of Integer division, Flowchart and its notations7. Typical C Program Development Environment, Role of Compiler and Linker8. Test Driving C Application9. Introduction to C Programming: A Simple C Program: Printing Text, Adding Two Integer10. Memory Concepts, Arithmetic in C, Operators11. Decision Making: Equality and Relational Operators12. Structured Program Development: The if, if...else, while Nested Control Statements13. Program Control: for, switch, do...while, break, continue, Logical Operators14. Functions: Modularizing Program in C, Math Library Functions15. Function Definitions and Prototypes, Function-Call Stack and Stack Frames16. Stack rolling and unrolling, Headers, Passing Arguments by Value and by Reference17. Random Number Generation, Scope Rules, Recursion, Recursion vs Iteration18. Arrays: Defining Arrays, Character Arrays, Static and Automatic Local Arrays19. Passing Arrays to Function, Sorting and Searching Arrays20. Multidimensional and Variable Length Arrays21. Pointers: Pointer Definitions and Initialization, Pointer Operators22. Passing Arguments to Function by Reference, Using the const and sizeof Operator23. Pointer Expressions and Arithmetic, Pointers and Arrays, Array of Pointers24. Function Pointers25. Characters and Strings: Strings and Characters, Character Handling Library26. String Functions, Library Functions27. Formatted Input/Output: Streams, Formatted Output with printf, Formatted Input with scanf28. Structures: Defining Structures, Accessing Structure Member, Structures and Functions29. typedef, Unions30. Bit Manipulation and Enumeration: Bitwise Operators, Bit Fields, Enumeration Constants31. File Processing: Files and Streams, Creating, Reading and Writing data to a Sequential and a Random-Access File32. Preprocessor: #include, #define, Conditional Compilation, #error and #pragma33. # and ## Operators, Predefined Symbolic Constants, Assertions34. Other Topics: Variable Length Argument List, Using Command Line Arguments35. Compiling Multiple-Source-File Programs, Program Termination with exit and atexit36. Suffixes for Integer and Floating-Point Literals, Signal Handling37. Dynamic Memory Allocation: calloc and realloc, goto38. Advance Topics: Self-Referential Structures, Linked Lists39. Efficiency of Algorithms, Selection and Insertion Sort
    CC-112›Characters and Strings: Strings and Characters, Character Handling Library
    Programming FundamentalsTopic 25 of 39

    Characters and Strings: Strings and Characters, Character Handling Library

    7 minread
    1,240words
    Intermediatelevel

    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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time7 min
      Word count1,240
      Code examples0
      DifficultyIntermediate