Write a program that will input three c-strings into an array of c-strings. Prin
ID: 641601 • Letter: W
Question
Write a program that will input three c-strings into an array of c-strings. Print only those that end in "es" or begin in "yan" but in different ways. For the last two characters, use a c-string function to find the length so you know where the last two characters are, then you can compare the last two characters separately against an e and against an s. For the first three characters, use a c-string function to compare, all at once, just the first three characters in the c-string (a function similar to strcmp, but that will only compare a certain number of characters). Use for loops to input and go through the array of three c-strings
Explanation / Answer
#include <stdio.h>
#include <string.h>
int main(){
int i = 0, length;
char arr[3][256];
int flags[3] = {0};
printf("Enter three strings ");
for(i = 0; i < 3; ++i){
scanf("%s", arr[i]);
length = strlen(arr[i]);
if(!strncmp(arr[i], "yan", 3)){
flags[i] = 1;
}
else if(arr[i][length - 2] == 'e' && arr[i][length - 1] == 's'){
flags[i] = 1;
}
}
printf(" ");
for(i = 0; i < 3; ++i){
if(flags[i]){
printf("%s ", arr[i]);
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.