Hi,
I've a string str as:
HiHello
I want to print only the Hello part i.e. from index 2 till the end like
print(str[3...endofstr]
How can I do that?
Thanks,
Angkar
Hi,
I've a string str as:
HiHello
I want to print only the Hello part i.e. from index 2 till the end like
print(str[3...endofstr]
How can I do that?
Thanks,
Angkar
That should do the trick.Code:for ( i = 2; i <= strlen( str ); i++ ) { printf("%c", str[i] ); }
p.s. sorry about the lack of tabs in my code
And what if I want to store the string str[3...endofstr] in some other variable?
~SvenCode:for( i=0, d=3; d < strlen( str ); i++,d++){ str2[i]=str1[d]; }
Windows XP Home Edition - Dev-C++ 4.9.9.2
board.theprogrammingsite.comOriginally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
Or you could make:
Code:{ strncpy(str2,&str1[2],str2_size); }
You can lose the for loop on that, if you're going to use printf():Originally Posted by bivhitscar
Of course, you should do bounds checking.Code:printf("%s", str + 2);
If you want to store it, another solution is:Code:if(strlen(str) >= 2) printf("%s", str + 2);
Edit: Note that the "%s" isn't needed in any of those, but if you leave it out, someone can type something like "blah %d %d", and trick s/printf().Code:sprintf(store_str, "%s", str + 2);
Last edited by Cactus_Hugger; 04-23-2006 at 07:47 PM.
long time; /* know C? */
Unprecedented performance: Nothing ever ran this slow before.
Any sufficiently advanced bug is indistinguishable from a feature.
Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
The best way to accelerate an IBM is at 9.8 m/s/s.
recursion (re - cur' - zhun) n. 1. (see recursion)
Or you could do something like this.
$gcc -Wall match.c -o matchCode:#include <stdio.h> #include <string.h> #include <stdlib.h> int main(int argc, char **argv) { if(argc !=2) exit(1); char temp[]="Hello"; size_t len_temp = strlen(temp); size_t len_argv = strlen(argv[1]); size_t len_diff = len_argv - len_temp; if(len_diff != 2) exit(1); if( strstr(argv[1],temp) ) printf("%s\n",temp); return 0; }
$./match HiHello
Hello
$./match MeHello
Hello
$./match NoooHello
$./match Hello
$./match ribbit
$./match Hell
$
Of course, the code is case sensitive.
Thanks to CDALTEN as his example taught me new thing i.e. size_t . It's really cool to use it instead of making new charecter type arrays.
REALNAPSTER
This will call strlen(str) for every iteration and so is much slower than if you only call strlen once.Originally Posted by 00Sven
Code:int len = strlen(str); for( i=0, d=3; d < len; i++,d++){ str2[i]=str1[d]; }
I think you're not understanding what they're doing. He's simply using a variable of type size_t to store the return value from the functions. Said functions return that type. It has nothing to do with "making new character type arrays" or not.
Quzah.
Hope is the first step on the road to disappointment.