C Program to Check Whether All Characters in First String is Present in Second String

This is a C Program to accept 2 string & check whether all characters in first string is present in second string & print.

Problem Description

This C Program checks whether all characters used in first string are present in second string.

Problem Solution

Take input from the user and perform string operations as shown in the program below.

Program/Source Code

Here is a source code of the C program to check whether all characters used in first string are present in the second string. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Accept 2 String & check whether all Characters
 * in first String is Present in second String & Print 
 */
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define CHAR_SIZE 26
 
void alphacheck(char *, int []);
void create(char *, int[]);
 
int main()
{
    char str1[50], str2[50];
    int a1[CHAR_SIZE] = {0}, a2[CHAR_SIZE] = {0}, i;
    char str1_alpha[CHAR_SIZE], str2_alpha[CHAR_SIZE];
 
    printf("Enter string1: ");
    scanf("%s", str1);
    printf("Enter string2: ");
    scanf("%s", str2);
    alphacheck(str1, a1);
    alphacheck(str2, a2);
    create(str1_alpha, a1);
    create(str2_alpha, a2);
    if (strcmp(str1_alpha, str2_alpha) == 0)
    {
        printf("All characters match in %s and %s.\n", str1, str2);
        printf("The characters that match are: ");
        for (i = 0; i < strlen(str1_alpha); i++)
        {
            printf("%c, ", str1_alpha[i]);
        }
        printf("\n");
    }
    else
    {
        printf("All characters do not match in %s and %s.\n", str1, str2);
    }
 
    return 0;
}
 
void alphacheck(char *str, int a[])
{
    int i, index;
 
    for (i = 0; i < strlen(str); i++)
    {
        str[i] = tolower(str[i]);
        index = str[i] - 'a';
        if (!a[index])
        {
            a[index] = 1;
        }
    }
}
 
void create(char *str, int a[])
{
    int i, j = 0;
 
    for (i = 0; i < CHAR_SIZE; i++)
    {
        if (a[i])
        {
            str[j++] = i + 'a';
        }
    }
    str[j] = '\0';
}
Program Explanation

In this C Program, we are reading two strings using ‘str1’ and ‘str2’ variables. The alphacheck() function is used to accept 2 strings & check whether all characters in first string is present in second string.

advertisement
advertisement

For loop the tolower() function is used to convert all the characters present in the string to lower case. Compute the difference between each character present in the ‘str[]’ array variable by ‘a’ character value. If condition statement is used to assign the value of ‘a[]’ array variable with base index ‘index’ variable to 1.

The create() function is used to accept 2 strings from str1[] and str2[] array variables respectively. For loop is used to assign the character value to the ‘str[]’ array variable.

If else condition statement is used to check the characters in first string is present in second string by using strcmp() function. If the condition is true then execute the statement, using for loop print the statement as the characters that match in the two strings.

Otherwise, if the condition is false, then execute the else statement. Print the statement as all characters do not match in the string1 and string2.

Runtime Test Cases
 
$ cc allchar.c 
$ ./a.out
Enter string1: aspired
Enter string2: despair
All characters match in aspired and despair.
The characters that match are: a, d, e, i, p, r, s,

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at programming examples on all topics, go to C Programming Examples.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.