String Subscript Problem

So I'm trying to do a program that short the name of a website
(ex: http://www.cplusplus.com to cplusplus.com)
But i get String Subscript out of range;
What's wrong?

1
2
3
4
5
for(decltype(WebSiteName.size()) index=0; index!=WebSiteName.size()
&& !isspace(WebSiteName[index]);index++)
	{
		WebSiteNameShort[index]=WebSiteName[index+cnt+4];
	}

Thanks in advance.
closed account (3qX21hU5)
Just search up to the '.' then remove everything infront of the period.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
 
using namespace std;

int main()
{
    string site = "www.cplusplus.com";
    
    int period = site.find_first_of('.');
    if (period != string::npos)
        site = site.substr(period + 1); // We need to add one to get past the period
        
    cout << site << endl;
    
}
Last edited on
I'll tell you if it works :)
It worked thanks, I'd like to know where i can find information about the functions you used, like ".find_first_of(), string::npos, substr()" .find_first_of() is kinda obvious but I don't understand the others and i only can be well with myself if i know what i did in the program.

Apologize my English.
closed account (3qX21hU5)
No problem, and here are some links to the stuff I used.

string.find_first_of()
http://www.cplusplus.com/reference/string/string/find_first_of/

string.npos
http://www.cplusplus.com/reference/string/string/npos/

string.substr()
http://www.cplusplus.com/reference/string/string/substr/


And all the other methods for strings can be found here
http://www.cplusplus.com/reference/string/string/


The only tricky one to understand is string::npos so I will explain it a bit. The way we used it in the example above is to check if the string.find_first_of() function found what it is was looking for. So lets say it didn't find a period in the string, it would then return the value string::npos from the function instead of the position where it found what we were looking for. So basically we use it to make sure the function found what it was looking for before we move on.
Last edited on
I'll take a look to this later, i have other program to do. Only 14 years old but time don't stop and i need to finish other program today.
Thanks
Last edited on
Topic archived. No new replies allowed.