Question #349684

Instructions:

  1. Check if the value of the char named myself is equal to one of the characters present in the string called others.
  2. Do this by comparing each of the characters in the string to myself.
  3. Print out only the index of the character which matches that of myself. If there are several matches, separate them by a single line.

Given code:

#include <iostream>

using namespace std;


int main() {

  // NOTE: Do not change this

  const char others[100] = "TheQuIckBrownFoxJumpedOvErTheLazyDog";

   

  char myself;


  // TODO: Ask the user for the value of `myself`

 

}

Sample input:

Enter myself: T


26


Expert's answer

#include <iostream>
using namespace std;

int main() {
  // NOTE: Do not change this
  const char others[100] = "TheQuIckBrownFoxJumpedOvErTheLazyDog";
   char myself;
   
   
  // TODO: Ask the user for the value of `myself`
    int i=0;
    cout<<"Enter char: ";
    cin>>myself;
    while(others[i]!='\0'){
        if(others[i]==myself)
            cout<<i<<endl;
        i++;
    }
 }
LATEST TUTORIALS
APPROVED BY CLIENTS