Question

In: Computer Science

I am trying to return a string from certain position of the sentence on C++, like...

I am trying to return a string from certain position of the sentence on C++, like a function/ statement that is equivalent to excel mid function.

Solutions

Expert Solution

#include <string.h>
#include <iostream>
using namespace std;

int main()
{
   string str ;
   cout<<"Enter sentence"<<endl;
   getline (cin, str);//to read a line with spaces

   int pos;
   cout<<"Enter position from where you want to get the string"<<endl;
   cin>>pos;
   int len=str.size();
   if(len<pos||pos<=0)
   {
       cout<<"Invalid position"<<endl;
       exit(0);
   }
int i=pos-1;
cout<<"String from position "<<pos<<" is => ";
if(str[i]==' '||str[i]=='   ')
{
   i++;
}
//break when you find terminating, space, tab or new line character
   while(str[i]!='\n'&&str[i]!=' '&&str[i]!='   '&&str[i]!='\0')
{
cout<<str[i];
i++;
}
cout<<endl;
cout<<"Sentence from position "<<pos<< " is => ";
cout<<str.substr(pos-1,len-1);
cout<<endl;
   return 0;
}

output 1-

Enter sentence
Hello Frank! What's going on?
Enter position from where you want to get the string
6
String from position 6 is => Frank!
Sentence from position 6 is => Frank! What's going on?

output 2-

Enter sentence
Hello Frank! What's going on?
Enter position from where you want to get the string
7
String from position 7 is => Frank!
Sentence from position 7 is => Frank! What's going on?


Related Solutions

IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX...
IN PROGRAMMING LANGUAGE C -I am trying to alphbetize a string in descending or to EX INPUT: B C D A OUTPUT: D C B A #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]) {         int MAX = 100000;         int i =0;         int k =0;         int j =0;         char array[MAX];         char split[] = " ,.-!?()0123456789";         int n = 0;         char second[MAX];         printf("Please enter in a String: ");...
I am trying to tokenize a string using a function by passing the char string[] and...
I am trying to tokenize a string using a function by passing the char string[] and char *pointer[100]. While I have working code inside the int main(), I am having trouble actually declaring the parameters for the function. I know how to pass the char array (char string[]), but not how to pass the char pointer array (char *pointer[100]). This is my code below: int main() {    // Declare variables    char str[] = "this is a test only...
I am trying to get this code to work but I am having difficulties, would like...
I am trying to get this code to work but I am having difficulties, would like to see if some one can solve it. I tried to start it but im not sure what im doing wrong. please explain if possible package edu.hfcc; /* * Create Java application that will create Fruit class and Bread class * * Fruit class will have 3 data fields name and quantity which you can change. * The third data field price should always...
Left shift. I am trying to left shift a string located in a text file. I...
Left shift. I am trying to left shift a string located in a text file. I am using c++ and the goal is to left shift by 1 character a string of length N, and output on stdout all of the performed shifts. Can someone tell me what I am doing wrong, and tell me what I can fix? i.e: text file contains: Hi there!. Output on stdout: Hi there!, i there!H, _there!Hi, there!Hi_,...., !Hi_there. #here "_" represent space. -------------------------------------------------------------------------------------------------------------------------...
Hey I am trying to find a way to find the position of a Red Dwarf...
Hey I am trying to find a way to find the position of a Red Dwarf star that is in an elliptical orbit at a certain time during it's orbit. I know the Red Dwarf's semi major axis which is 3 AU (astronomical unit) and it's period is 1896 days. I've tried to do a simple using its orbit velocity and doing distance=velocity x time, but I wasn't getting answers that made sense. If anyone could set me on the...
In C programming, I am trying to search for the names of people that in this...
In C programming, I am trying to search for the names of people that in this DOISigned.txt file, however I am having trouble getting the first and last names of the multiple people named john, my current code only searches for John once and then it terminates,here is my current code #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <string.h> #include <stdlib.h> #define BUF_SIZE 0x3000 char buf[BUF_SIZE]; int main() {    char* inputFile = "DOISigners.txt";    FILE* fp;    fp = fopen(inputFile, "r");...
I am trying to write a code in C for a circuit board reads in a...
I am trying to write a code in C for a circuit board reads in a temperature from a sensor on the circuit board and reads it onto a 7-segment LED display. How exactly would you code a floating 2 or 3 digit reading onto the LED display? I am stuck on this part.
I am trying to solve a c++ problem over c strings where I have to input...
I am trying to solve a c++ problem over c strings where I have to input an email address and check if it is valid. After completing my code I keep getting errors that my program will not run, specifically the lines with my for loops. Can you please look at my code and tell me what is wrong and how I can fix the code? I included below the assignment instructions and my current code. Assignment Instructions Write a...
I am trying to implement a search function for a binary search tree. I am trying...
I am trying to implement a search function for a binary search tree. I am trying to get the output to print each element preceding the the target of the search. For example, in the code when I search for 19, the output should be "5-8-9-18-20-19" Please only modify the search function and also please walk me through what I did wrong. I am trying to figure this out. Here is my code: #include<iostream> using namespace std; class node {...
Hello, I am trying to write a C++ program that will do the following: Use the...
Hello, I am trying to write a C++ program that will do the following: Use the STL stack container in a program that reads a string, an arithmetic expression to be exact, one character at a time, and determines if the string has balanced parenthesis – that is, for each left parenthesis there is exactly one matching right parenthesis later in the string.                         Use the following strings to test your program. A+ B - C A * B / (C...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT