This program should input 3 c-strings into an array of c-strings. Print only tho
ID: 3543477 • Letter: T
Question
This program should input 3 c-strings into an array of c-strings.
Print only those that end is "es" or begin in "yan".
1st. Use c-string function to find the length so you know where the last two characters are, then compare the last two characters separately.
2nd. 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 loops.
I should not use string objects.
This one I was just trying it around. So if it is confusing just ignore it
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
void main()
{
int len;
char words;
string beg = "yan";
string end = "es";
string test;
int i;
size_t j;
// inputing the string
for (i = 0; i < 3 ; i++)
{
cout << "input string: " << endl;
len = strlen(words);
if (strcmp((words.substr(0, 2), beg ) == 0))
cout << words[i];
else
strstr(words,end);
}
}
Explanation / Answer
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
int main()
{
int len;
char words[45];
char* beg = "yan";
char* end = "es";
int i;
size_t j;
// inputing the string
for (i = 0; i < 3 ; i++)
{
cout << " input string: ";
cin >> words;
cout << endl;
len = strlen(words);
if(words[len-2]=='e' && words[len-1]=='s')
{
cout << words;
}
else if (strncmp(words,beg,3) == 0)
{
cout << words;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.