How to do substring to get the last 4characters in c

let says i have char *yytext = "p103_stb" 0r "p0_stb" or "p100_stb"




How to do substr to get the "_stb" in C?
well, I would use std::string for this, unless of course you mean "pure" c, and not c++.
1
2
3
4
5
6
string vre_pin = yytext;
	cout << "chunhaun1:" << vre_pin <<endl;
	unsigned position = vre_pin.find("_stb");
	cout << "pos:" << position <<endl;
	string is_stb = vre_pin.substr(position);
	cout << "chunhaun2:" << is_stb << endl;




Hi this is what i tried using c++.but error occur.could you help me check with my problem?The example of string will be just same as above.
Why my yytext content are either the axample above when i print vre_pin.size() i got all the value with 4294967295?

Error:
pos:4294967295
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr
Abort
Last edited on
well, this works on my id, not sure why you are getting out of range, as position should be 4, and, that is in range.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int _tmain(int argc, _TCHAR* argv[])
{
	
	char yytext[9] = "p103_stb";
	
	std::string vre_pin = yytext;
	std::cout << "chunhaun1:" << vre_pin <<std::endl;
	unsigned position = vre_pin.find("_stb");
	std::cout << "pos:" << position <<std::endl;
	std::string is_stb = vre_pin.substr(position);
	std::cout << "chunhaun2:" << is_stb << std::endl;

	
	
	
	return 0;
}


btw, you could also just use

std::string vre_pin = "p103_stb";

this would eliminate the need to create a static array, which I find quite annoying, as it is, well, static
I am working with lex together. So when my yytext match the string when lex scan thru a file.

so yytext will read during run time.Therefore,I cannot direct assign string to yytext.

But i wonder why when i print out the position will appear out of range error.
Or should i do somethg like chomp off space,newline after the string just like perl.
so, basically, when you execute

 
unsigned position = vre_pin.find("_stb");


you get a position of 4294967295?

That does seem awkward to me.

Try using the code I posted, as it is slightly different from yours. Then post what happens!

Ya.i print out the position all is the value 4294967295.


I did try your code.It happened the same. :(

I am using g++ in unix to compile.
hmm, well, not sure why this is happening. my ide is visual 2012, and the code I posted works. maybe someone else has any input??
@jaden5165

let says i have char *yytext = "p103_stb" 0r "p0_stb" or "p100_stb"

How to do substr to get the "_stb" in C?


There are several approaches. For example

const char *p = yytext + strlen( yytext ) - strlen( "_stb" );

or

const char *p = strstr( yytext, "_stb" );
@vlad

Hi
1
2
3
4
printf("yytext:%s\n",yytext);
					char *p ;
					p= strstr(yytext,"_stb");
					printf("chunhaun:%s\n",p);





yytext:p2_drv
chunhaun:(null)
yytext:p4_stb
chunhaun:_stb


ya it works. Could you tell me why i cant use http://www.cplusplus.com/forum/general/101490/#msg545497
Last edited on
string find wrote:
Return value:

The position of the first character of the first match.
If no matches were found, the function returns string::npos.

1
2
3
4
5
char *p ;
					p= strstr(yytext,"_stb");
					if( ! strcmp(p,"_stb") ){
					printf("chunhaun:%s\n",p);
					}




What's wrong to this code.It cause segmentation fault.
Same problem as with the string::find : if strstr returns null pointer, then calling strcmp is not safe.
Topic archived. No new replies allowed.