This is a C Program to accept 2 string & check whether all characters in first string is present in second string & print.
This C Program checks whether all characters used in first string are present in second string.
Take input from the user and perform string operations as shown in the program below.
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'; }
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.
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.
$ 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.
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
- Practice Computer Science MCQs
- Apply for C Internship
- Watch Advanced C Programming Videos
- Check Computer Science Books
- Apply for Computer Science Internship