You are on page 1of 3

Bohol Island State University

College of Engineering and Architecture

CPE 112 Programming Logic and Design

Strings - Activity #01

Name: Camille S. Rule Date: May 16 2023 Score: ___________________

Problem #1

Input 1:

Black

Brick

Output 1:

Bck

Input 2:

Stray

Stay
Output 2:

Stay

Code:

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

void printCommonLetters(const char* word1, const char* word2) {


int len1 = strlen(word1);
int len2 = strlen(word2);
int letters[26] = {0}; // Assuming only lowercase English letters

// Count the occurrences of letters in the first word


int i;
for ( i = 0; i < len1; i++) {
letters[word1[i] - 'a']++;
}

// Check the occurrence of letters in the second word and print common letters
printf("Common letters: ");
for (i = 0; i < len2; i++) {
if (letters[word2[i] - 'a'] > 0) {
printf("%c ", word2[i]);
letters[word2[i] - 'a']--;
}
}
printf("\n");
}

int main() {
char word1[100], word2[100];

printf("Enter the first word: ");


scanf("%s", word1);

printf("Enter the second word: ");


scanf("%s", word2);

printCommonLetters(word1, word2);

return 0;
}

You might also like