Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Update that input three c-strings and printed those that began with \"yan\" or e

ID: 3676879 • Letter: U

Question

Update that input three c-strings and printed those that began with "yan" or ended with "es" using loops and c-string functions. Modify it so string objects are used instead of c-strings. Input three strings into an array of strings. Again use a function to first find the length for the end comparisons, then you can again compare the last two characters separately with a string function. Then use a string function to take a sub-string of the first three characters for the beginning comparison all at once. Use separate for loops to input and go through the array.

#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;
}

Explanation / Answer

Few points are not clear but I did some modifications, please see if this works for you or feel free to comment back

#include<iostream>
#include<string>
int last_cmp(string name){
int length = name.size();
  
if (name[length - 1] == 's' & name[length - 2] == 'e')
return 1;
}
int first_cmp(string name){
string sub = name.substr(0,3);
cout << sub;
if(sub=="yan")
return 1;
else return 0;
}
int main()
{

string name[3];
int i;
for (i = 0; i<3; i++){
getline(cin, name[i]);   

}
  
for (i = 0; i<3; i++){
if (last_cmp(name[i]) == 1)
cout << "String ends with es: "<< name[i]<<endl;
if (first_cmp(name[i]) == 1)
cout << "String starts with yan: "<< name[i] << endl;
}
return 0;
  
  
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote