Write a c++ program that will input three c-strings into an array of c-strings.
ID: 3676649 • Letter: W
Question
Write a c++ 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 using []. 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 separate for loops to input and go through the array of three c-strings. Attach the .cpp file and submit.
Explanation / Answer
#include <stdio.h>
#include <string.h>
int last_cmp(char name_array[]){
int length = strlen(name_array);
if (name_array[length-2] == 's' & name_array[length-3] == 'e')
return 1;
}
int first_cmp(char name_array[]){
if (name_array[0] == 'y' & name_array[1] == 'a' & name_array[2] == 'n')
return 1;
}
int main()
{
char name[3][100], i=1;
for (i=0;i<3;i++){
printf("Enter your name: ");
fgets (name[i], 100, stdin);
}
for (i=0;i<3;i++){
if (last_cmp(name[i]) ==1)
printf("String ends with es: %s",name[i]);
if (first_cmp(name[i]) ==1)
printf("String starts with yan: %s",name[i]);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.