String and Character Library Functions

The C programming language provides a variety of string and character manipulation functions in its standard library, which is commonly referred to as the C Standard Library. Here are some commonly used string and character functions, along with sample code:

1. strlen (String Length)

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello, World!";
    int length = strlen(str);

    printf("Length of the string: %d\n", length);

    return 0;
}

2. strcpy (String Copy)

#include <stdio.h>
#include <string.h>

int main() {
    char source[] = "Hello, World!";
    char destination[20];

    strcpy(destination, source);

    printf("Copied string: %s\n", destination);

    return 0;
}

3. strcat (String Concatenation)

#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello, ";
    char str2[] = "World!";

    strcat(str1, str2);

    printf("Concatenated string: %s\n", str1);

    return 0;
}

4. strcmp (String Comparison)

#include <stdio.h>
#include <string.h>

int main() {
    char str1[] = "Hello";
    char str2[] = "World";

    int result = strcmp(str1, str2);

    if (result == 0)
        printf("Strings are equal\n");
    else if (result < 0)
        printf("String 1 is less than String 2\n");
    else
        printf("String 1 is greater than String 2\n");

    return 0;
}

5. strchr (Find Character in 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 + 1);
    else
        printf("Character not found\n");

    return 0;
}

6. strstr (Find Substring in String)

#include <stdio.h>
#include <string.h>

int main() {
    char str[] = "Hello, World!";
    char *ptr = strstr(str, "World");

    if (ptr != NULL)
        printf("Substring found at position: %ld\n", ptr - str + 1);
    else
        printf("Substring not found\n");

    return 0;
}

7. gets (Get String)

#include <stdio.h>

int main() {
    char str[50];

    printf("Enter a string: ");
    gets(str);

    printf("You entered: %s\n", str);

    return 0;
}

8. puts (Put String)

#include <stdio.h>

int main() {
    char str[] = "Hello, World!";

    puts("String to be printed: ");
    puts(str);

    return 0;
}

9. getchar (Get Character)

#include <stdio.h>

int main() {
    char ch;

    printf("Enter a character: ");
    ch = getchar();

    printf("You entered: %c\n", ch);

    return 0;
}

10. putchar (Put Character)

#include <stdio.h>

int main() {
    char ch = 'A';

    printf("Character to be printed: ");
    putchar(ch);

    return 0;
}

11. isalpha (Is Alphabet)

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

12. isdigit (Is Digit)

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

13. isalnum (Is Alphanumeric)

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

14. toupper and tolower (Convert to Upper/Lower Case)

#include <stdio.h>
#include <ctype.h>

int main() {
    char ch = 'a';

    printf("Uppercase: %c\n", toupper(ch));
    printf("Lowercase: %c\n", tolower(ch));

    return 0;
}
0
Subscribe to my newsletter

Read articles from Jyotiprakash Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Jyotiprakash Mishra
Jyotiprakash Mishra

I am Jyotiprakash, a deeply driven computer systems engineer, software developer, teacher, and philosopher. With a decade of professional experience, I have contributed to various cutting-edge software products in network security, mobile apps, and healthcare software at renowned companies like Oracle, Yahoo, and Epic. My academic journey has taken me to prestigious institutions such as the University of Wisconsin-Madison and BITS Pilani in India, where I consistently ranked among the top of my class. At my core, I am a computer enthusiast with a profound interest in understanding the intricacies of computer programming. My skills are not limited to application programming in Java; I have also delved deeply into computer hardware, learning about various architectures, low-level assembly programming, Linux kernel implementation, and writing device drivers. The contributions of Linus Torvalds, Ken Thompson, and Dennis Ritchie—who revolutionized the computer industry—inspire me. I believe that real contributions to computer science are made by mastering all levels of abstraction and understanding systems inside out. In addition to my professional pursuits, I am passionate about teaching and sharing knowledge. I have spent two years as a teaching assistant at UW Madison, where I taught complex concepts in operating systems, computer graphics, and data structures to both graduate and undergraduate students. Currently, I am an assistant professor at KIIT, Bhubaneswar, where I continue to teach computer science to undergraduate and graduate students. I am also working on writing a few free books on systems programming, as I believe in freely sharing knowledge to empower others.