Home > Back-end >  How to print substring contained between braces?
How to print substring contained between braces?

Time:09-26

I'm writing a function, which is supposed to output whatever is between curly braces in string
(e.g. hello world for text{hello world}).
But it isn't outputting anything when I try running it. Do you have any idea what could be wrong?

#include <iostream>
#include <string>

using namespace std;

int main(){
    start:    
    std::string funct;
    
    std::cout << "-->";
    std::cin >> funct;
    
    int temp = 0;

//focus on what's down here V
    
    string word;
    if (funct.find("text") != string::npos)
        {
            for(int i=0; i<1; i){
                if(funct.at(temp)=='}'){i=1;}
                    else{
                    temp  = temp;
                    word = funct.at(temp   5);}
            }
        cout<<word<<endl;    
        }
    
    cout<<endl<<endl<<"=================================================="<<endl<<endl;
    goto start;
    
    return 0;
}

*Edit, 'funct' is the raw user input, and 'temp' is just a placeholder integer.

CodePudding user response:

As I understand, you want a function that is parsing a string and printing whatever is within a construction like 'text{...}' Here is a few issues in your code:

  1. std::cin >> funct will only read a word (whitespace separated text). If you want to get an entire line, use std::getline(std::cin, yourString)

  2. You look for an occurrence of 'text', but do not save the index:

    if (funct.find("text") != string::npos)
  1. std::string::at() is indeed a safe way of accessing an element of a string, however your program may terminate with std::out_of_range exception if not handled.

  2. std::string::at() returns not a string, but a char&

    word = funct.at(temp 5);

Here's my implementation of a function, that finds text{...} construct in a string and prints the contents:

#include <iostream>
//using reference, so that the object is not copied
void print_text(const std::string& string) {
        //starting and ending points of the needed text
        size_t start = string.find("text"),
        end;
        //if "text" is not found, exit function
        if (start == std::string::npos)
                return;
        //adding to start, so that it points to the last character of "test"
        start  = sizeof("text") - 2;
        try {
                //ignoring all the spaces after "text"
                while (isspace(string.at(  start)));
                //testing for the open brace before the text
                if (string.at(start) != '{')
                        return;
                end = start;
                  start;
                //testing for the closing brace after the text needed
                while (string.at(  end) != '}');
                //printing the substring if all the conditions are met
                std::cout <<  string.substr(start, end - start) << '\n';
        }
        //any time we reach the end of the string, we bail printing nothing
        catch(std::out_of_range) {
                return;
        }
}

int main() {
        print_text(std::string("sample text{test1}"));
        print_text(std::string("sample space text {test2}"));
        print_text(std::string("empty sample text{}"));
        print_text(std::string("sample text{test4} some after"));
        print_text(std::string("sample forgot closing text{test5"));
        print_text(std::string("sample forgot starting texttest6}"));
        print_text(std::string("sample forgot starting text test7}"));
        print_text(std::string("sample extra characters text hi {test8}"));
}

Output:

test1
test2

test4

Note: You can effectively ask a user input in the main function and pass the string in print_text function, it would work. Keep in mind, that it would only print the first occurrence of text{}, if you need any number of occurrences found, comment this reply.

  • Related