Question

In: Computer Science

This is a c++ code. Write a series of assignment statements (complete executable code) that find...

This is a c++ code. Write a series of assignment statements (complete executable code) that find the first three positions of the string “and” in a string variable sentence. The positions should be stored in int variables called first, second and third. You may declare additional variables if necessary. The contents of sentence should remain unchanged. The sentence can be completely random.

Im having a large problem with this. It's a simple c++ code, but im unable to get it. Thank you.

Solutions

Expert Solution

#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
using namespace std;
static const char alpha[]="abc def ghi jkl mno pqr stu vwx yz";
int len=sizeof(alpha)-1;
char genRandom() // Random string generator function.
{
    return alpha[rand()%len]; //selecting single char
}

int main()
{
string str;
int p[3],j=0,first,second,third;
srand(time(0));
    for(int z=0; z < 50; z++)
    {
        str=str+genRandom(); //adding of random char for forming string
    }
cout<<"Randomly generated Sentence is: ";
cout <<str<<endl<<endl;
int n=str.length(); //length of Randomly generated Sentence
p[0]=p[1]=p[2]=0;
for(int i=0;i<n-2;i++)   //checking for and in Randomly generated Sentence
{
  if(str[i]=='a')
  {
   if(str[i+1]=='n')
   {
    if(str[i+2]=='d')
    {
     p[j]=i+1;   //storing of position of and after finding
     j++;
     i=i+3;
    }
   }
  }
}
first=p[0];
second=p[1];
third=p[2];

//printing of positions
cout<<"First position of and is: "<<first<<endl;
cout<<"Second position of and is: "<<second<<endl;
cout<<"Third position of and is: "<<third<<endl;
}


output:-
1)
Randomly generated Sentence is: d lvk vvxzkakwvgbhhe fbmpurpjxqppvbmb u v wg ceai

First position of and is: 0
Second position of and is: 0
Third position of and is: 0


Process exited normally.
Press any key to continue . . .

2)
Randomly generated Sentence is:   klo smj adgmdgpujtkjes v wy w eyx kbwfumt ekrw

First position of and is: 0
Second position of and is: 0
Third position of and is: 0


Process exited normally.
Press any key to continue . . .

3)
Randomly generated Sentence is:    hub upn ov ixppldiemll wyy qv o gtezf k n   bk

First position of and is: 0
Second position of and is: 0
Third position of and is: 0


Process exited normally.
Press any key to continue . . .


Related Solutions

Write a code on C++ to find prime number series.
Write a code on C++ to find prime number series.
Please code in C# - (C - Sharp) Assignment Description Write out a program that will...
Please code in C# - (C - Sharp) Assignment Description Write out a program that will ask the user for their name; the length and width of a rectangle; and the length of a square. The program will then output the input name; the area and perimeter of a rectangle with the dimensions they input; and the area and perimeter of a square with the length they input. Tasks The program needs to contain the following A comment header containing...
Download Jupyter and complete your Assignment. Be sure to write and run the code separately for...
Download Jupyter and complete your Assignment. Be sure to write and run the code separately for each part. Please provide explanation for the code logic behind the code you write / design. First part: Create Simple Student Class with constructor to get first name and last name. Second part: Then create Address class with the constructor to get Student Address, the parameters can be , House/Apt Number, Street, Zip, State. Third part: Modify Student Class so that it will get...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
In this programming assignment, you will write C code that performs recursion. For the purpose of...
In this programming assignment, you will write C code that performs recursion. For the purpose of this assignment, you will keep all functions in a single source file main.c. Your main job is to write a recursive function that generates and prints all possible password combinations using characters in an array. In your main() function you will first parse the command line arguments. You can assume that the arguments will always be provided in the correct format. Remember that the...
C++ Write the code to implement a complete binary heap using an array ( Not a...
C++ Write the code to implement a complete binary heap using an array ( Not a vector ). Code for Max heap. Implement: AddElement, GetMax, HeapSort, ShuffleUp, ShuffleDown, etc Set array size to 31 possible integers. Add 15 elements 1,3,27,22,18,4,11,26,42,19,6,2,15,16,13 Have a default constructor that initializes the array to zeros.. The data in the heap will be double datatype. PART 2 Convert to the program to a template, test with integers, double and char please provide screenshots thank you so...
I need the c# code for the below assignment. Complete PigLatin program in Windows Forms GUI....
I need the c# code for the below assignment. Complete PigLatin program in Windows Forms GUI. Zip the solution project file and attach to this submission. Do this in the GUI format (Windows Form). Be sure and add a Clear Button to reset for entering another word or words. PigLatinGUI Basic Steps in Creating your Program Create your user interface (GUI). Use different font styles but don’t overdo it, Use colors, and Use graphics (You can find all kinds of...
Using the conditional assignment statements, write the verilog code for 16:1 Mux. Write the test bench...
Using the conditional assignment statements, write the verilog code for 16:1 Mux. Write the test bench for this module.
In C++ Complete the following code without the use of auto. Write a function, getAverages, that...
In C++ Complete the following code without the use of auto. Write a function, getAverages, that will prompt the user for a series of exam averages for an unspecified number of students in a class. The user should enter a negative exam average to indicate the end of data. Exam averages should be stored in the vector indicated by the reference parameter. Write a function, storeAverages, that will accept two parameters, a vector containing exam averages and a string containing...
For this computer assignment, you are to write and implement an interactive C++ program to find...
For this computer assignment, you are to write and implement an interactive C++ program to find and print all prime numbers, which are less than or equal to a given value of n, using the algorithm known as the Sieve of Eratosthenes. A prime number p is an integer greater than 1 that is divisible only by 1 and p (itself). The algorithm begins by initializing a set container to contain all the integers in the range 2 to n....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT