-3

I've one variable in which i get string value from user. Now i want to use that string value in URL. like my string value is "Indore". so it should pass like URL="awe/Indore/ddsd". Please tell me how to solve?

Bond
  • 25
  • 6
  • Neither c nor c++ have a basic notion of URLs. It's completely unclear what you're asking about, sorry. – πάντα ῥεῖ Jun 06 '19 at 17:58
  • 2
    Welcome to StackOverflow! What have you tried already? How did it go and what error messages did you receive? – Kyle Willmon Jun 06 '19 at 17:58
  • Your question is absolutely vague, but I guess you should take a look at `strcat` method – Ehsan Khodarahmi Jun 06 '19 at 18:16
  • @Bond Maybe `std::regex` can provide you with some useful operations to insert to the URL that you finally need. – πάντα ῥεῖ Jun 06 '19 at 18:18
  • @Bond Your question is just as unclear as ever. Please add clarifications to the question itself, along with example code showing what you have tried. – JaMiT Jun 06 '19 at 23:15
  • I just want to clarify. I've one variable var1 which gets value from user. e.g. var1 = "Russia" and i've another variable var2="Capital/Japan". Output should be "Capital/Russia/Japan" Note : I used c_str() but got below mentioned output: "Capital/var1.c_str()/Japan" instead of "Capital/Russia/Japan". – Bond Jun 07 '19 at 22:44

1 Answers1

1

I think first we need to know what "String" type are you working with and maybe provide examples so that we can better help you. From what I see from your comment, you can split the string first then merge them together.

If it is a C string char* then you can use strtok to split (see another topic). Then merge with strcat as Ehsan Khodarahmi mentioned, (see another topic)

If it is a std::string string, then you can try my implementation in c++ for splitting std::string string.

std::vector<std::string> splitString(std::string inputString, std::string delimiter){
    std::string temp = inputString;
    std::vector<std::string> output;

    size_t pos = 0;
    std::string token;
    while ((pos = temp.find(delimiter)) != std::string::npos) {
        token = temp.substr(0, pos);
        output.push_back(token);
        temp.erase(0, pos + delimiter.length());
    }
    output.push_back(temp);
    return output;
}

In your case, you can do

std::vector<std::string> splitted = spitString("Capital/Madhya", "/");

Then splitted = {"Capital", "Madhya"}, and you can just do

std::string url = splitted[0] + "/Indore/" + splitted[1];

As people said in the comment, your question is vague, so my answer may not directly help you.

Update

If you are using c_str() then you are probably using std::string. c_str() is used for converting std::string to c string char* (see reference). So in this case, you can use my code provided to assist you. From what you told me, user input was passed to var1.

std::string var1 = "Russia"

And you already have var2

std::string var2 = "Capital/Japan"

Then simply split the var2 by / and merge what you want.

std::vector<std::string> splitted = splitString(var2, "/");
std::string result = splitted[0] + "/" + var1 + "/" + splitted[1];

Then your result should be "Capital/Russia/Japan".

Update2

First, thanks for include code this time. You should do that all the time if possible so people know what to work with. Note: To format your code, enclose your code with ```

Now to your question. You provided this

#include <iostream> 
#include <vector> 

using namespace std;

int main() { 
    std::string var1 = "Russia"; 
    std::string var2 = "Capital/Japan"; 
    std::vector<std::string> splitted = splitString(var2, "/"); 
    std::string result = splitted[0] + "/" + var1 + "/" + splitted[1]; 
    cout<<result; return 0; 
} 

And your compiler doesn't like it and says

error: 'splitString' was not declared in this scope

This error message basically says that splitString function does not exist. This is because you did not include my code in your code. At some point you will get more comfortable reading these error messages.

So the fix is

#include <iostream> 
#include <vector> 

using namespace std;

std::vector<std::string> splitString(std::string inputString, std::string delimiter){
    std::string temp = inputString;
    std::vector<std::string> output;

    size_t pos = 0;
    std::string token;
    while ((pos = temp.find(delimiter)) != std::string::npos) {
        token = temp.substr(0, pos);
        output.push_back(token);
        temp.erase(0, pos + delimiter.length());
    }
    output.push_back(temp);
    return output;
}

int main() { 
    std::string var1 = "Russia"; 
    std::string var2 = "Capital/Japan"; 
    std::vector<std::string> splitted = splitString(var2, "/"); 
    std::string result = splitted[0] + "/" + var1 + "/" + splitted[1]; 
    cout<<result; return 0; 
} 
  • I just want to clarify. I've one variable var1 which gets value from user. e.g. var1 = "Russia" and i've another variable var2="Capital/Japan". Output should be "Capital/Russia/Japan" Note : I used c_str() but got below mentioned output: "Capital/var1.c_str()/Japan" instead of "Capital/Russia/Japan". – Bond Jun 07 '19 at 22:45
  • Please check me updated answer. – Zachary Liu Jun 08 '19 at 00:10
  • Code is : #include #include using namespace std; int main() { std::string var1 = "Russia"; std::string var2 = "Capital/Japan"; std::vector splitted = splitString(var2, "/"); std::string result = splitted[0] + "/" + var1 + "/" + splitted[1]; cout< splitted = splitString(var2, "/"); ^ – Bond Jun 09 '19 at 19:13
  • Please check me updated answer. – Zachary Liu Jun 09 '19 at 22:11