Dremendo Tag Line

String Methods in C Programming

String in C

In this lesson, we will learn about the different types of string methods in C programming and their uses with the help of examples.

What are String Methods in C

String methods are a collection of useful functions applied on a character array to solve various string related questions. Let's see all the important string methods one by one with examples.

video-poster

strlen() Method

The strlen() method returns the length of the string without including null character ('\0'). The function is defined in <string.h> header file.

Example

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

int main()
{
    char s[30];
    printf("Enter any string : ");
    gets(s);
    printf("Length of the string : %d",strlen(s));
    return 0;
}

Output

Enter any string : Hello World
Length of the string : 11

strcmp() Method

The strcmp() method compares the two strings and returns an integer value. If both the strings are same (equal) then this function would return 0 otherwise it may return a negative or positive value based on the comparison. The function is defined in <string.h> header file.

Example

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

int main()
{
    char str1[30]="Mango";
    char str2[30]="Mango";

    if(strcmp(str1,str2)==0)
    {
        printf("str1 and str2 both are same");
    }
    else
    {
        printf("str1 and str2 are not same");
    }
    return 0;
}

Output

str1 and str2 both are same

strcmpi() Method

The strcmpi() method compares the two strings without their case (upper and lower case) and returns an integer value. If both the strings are same (equal) then this function would return 0 otherwise it may return a negative or positive value based on the comparison. The function is defined in <string.h> header file.

Example

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

int main()
{
    char str1[30]="Mango";
    char str2[30]="MaNgO";

    if(strcmpi(str1,str2)==0)
    {
        printf("str1 and str2 both are same");
    }
    else
    {
        printf("str1 and str2 are not same");
    }
    return 0;
}

Output

str1 and str2 both are same

strcat() Method

The strcat() method concatenates two strings and returns the concatenated string. The function is defined in <string.h> header file.

Example

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

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

    strcat(str1,str2);
    printf("String after concatenation: %s",str1);
    return 0;
}

Output

String after concatenation: HelloWorld

strcpy() Method

The strcpy() method copies the string str2 into string str1, including null character ('\0'). The function is defined in <string.h> header file.

Example

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

int main()
{
    char str1[50]="string 1";
    char str2[50]="string 2";

    printf("str1 before copy: %s\n",str1);
    /* this function has copied str2 into str1*/
    strcpy(str1,str2);
    printf("str1 after copy: %s",str1);
    return 0;
}

Output

str1 before copy: string 1
str1 after copy: string 2

strupr() Method

The strupr() method converts a string to uppercase. The function is defined in <string.h> header file.

Example

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

int main()
{
    char str1[30]="hello world";

    // convert the str1 into uppercase
    strupr(str1);
    printf("str1 is: %s",str1);
    return 0;
}

Output

str1 is: HELLO WORLD

strlwr() Method

The strlwr() function converts a string to lowercase. The function is defined in <string.h> header file.

Example

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

int main()
{
    char str1[30]="HELLO WORLD";

    // convert the str1 into lowercase
    strlwr(str1);
    printf("str1 is: %s",str1);
    return 0;
}

Output

str1 is: hello world

strrev() Method

The strrev() method converts a string into reverse order. The function is defined in <string.h> header file.

Example

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

int main()
{
    char str1[30]="HELLO WORLD";

    // convert the str1 into reverse order
    strrev(str1);
    printf("str1 is: %s",str1);
    return 0;
}

Output

str1 is: DLROW OLLEH

tolower() Method

The tolower() method converts a character to lowercase. The function is defined in <ctype.h> header file.

Example

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

int main()
{
    char str1='H';

    str1=tolower(str1);
    printf("str1 is: %c",str1);
    return 0;
}

Output

str1 is: h

toupper() Method

The toupper() method converts a character to uppercase. The function is defined in <ctype.h> header file.

Example

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

int main()
{
    char str1='h';

    str1=toupper(str1);
    printf("str1 is: %c",str1);
    return 0;
}

Output

str1 is: H

isupper() Method

The isupper() method returns non-zero if the character is an uppercase letter. Otherwise, zero is returned. The function is defined in <ctype.h> header file.

Example

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

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

    if(isupper(str1)!=0)
    {
        printf("str1 is in uppercase");
    }
    else
    {
        printf("str1 is not in uppercase");
    }
    return 0;
}

Output

str1 is in uppercase

islower() Method

The islower() method returns non-zero if the character is a lowercase letter. Otherwise, zero is returned. The function is defined in <ctype.h> header file.

Example

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

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

    if(islower(str1)!=0)
    {
        printf("str1 is in lowercase");
    }
    else
    {
        printf("str1 is not in lowercase");
    }
    return 0;
}

Output

str1 is in lowercase

isalpha() Method

The isalpha() method returns non-zero if the character is an alphabet letter. Otherwise, zero is returned. The function is defined in <ctype.h> header file.

Example

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

int main()
{
    char str1='g';

    if(isalpha(str1)!=0)
    {
        printf("str1 is an alphabet");
    }
    else
    {
        printf("str1 is not an alphabet");
    }
    return 0;
}

Output

str1 is an alphabet

isdigit() Method

The isdigit() method returns non-zero if the character is a digit between 0 and 9. Otherwise, zero is returned. The function is defined in <ctype.h> header file.

Example

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

int main()
{
    char str1='5';

    if(isdigit(str1)!=0)
    {
        printf("str1 is a digit");
    }
    else
    {
        printf("str1 is not a digit");
    }
    return 0;
}

Output

str1 is a digit

ispunct() Method

The ispunct() method returns non-zero if the character is a printing character but not an alphabet, digit or a space. Otherwise, zero is returned. The function is defined in <ctype.h> header file.

Example

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

int main()
{
    char str1='?';

    if(ispunct(str1)!=0)
    {
        printf("str1 is a punctuation");
    }
    else
    {
        printf("str1 is not a punctuation");
    }
    return 0;
}

Output

str1 is a punctuation

isspace() Method

The isspace() method returns non-zero if the character is some sort of space (i.e. single space, tab, vertical tab, form feed, carriage return, or newline). Otherwise, zero is returned. The function is defined in <ctype.h> header file.

Example

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

int main()
{
    char str1=' ';

    if(isspace(str1)!=0)
    {
        printf("str1 is a space");
    }
    else
    {
        printf("str1 is not a space");
    }
    return 0;
}

Output

str1 is a space

atoi() Method

The atoi() method converts string into an integer, and returns that integer. The string should start with whitespace or some sort of number, and atoi() function will stop reading from string as soon as a non-numerical character has been read. The function is defined in <stdlib.h> header file.

Example

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main()
{
    int x;
    x=atoi("512");
    printf("value of x = %d",x);
    return 0;
}

Output

value of x = 512

atol() Method

The atol() method converts string into a long, then returns that value. The atol() function will read from string until it finds any character that should not be in a long. The resulting truncated value is then converted and returned. The function is defined in <stdlib.h> header file.

Example

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main()
{
    long x;
    x=atol("85642");
    printf("value of x = %ld",x);
    return 0;
}

Output

value of x = 85642

atof() Method

The atof() method converts string into a double, then returns that value. The string must start with a valid number, but can be terminated with any non-numerical character, other than "E" or "e". The function is defined in <stdlib.h> header file.

Example

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

int main()
{
    double x;
    x=atof("7462.3659");
    printf("value of x = %lf",x);
    return 0;
}

Output

value of x = 7462.365900

Test Your Knowledge

Attempt the practical questions to check if the lesson is adequately clear to you.

Test Your Knowledge