better still use strstr(char *str1, char *str2)
Main Topics
Browse All TopicsHow to get substring in C
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
char* points to an array of characters. Conventionally in C, there is an ASCII 0 ('\0') character in character arrays to denote the end.
If, say, you declare,
char *p = "abcd";
your p character pointer points to an array of 5 (yes 5) characters, the last of which is '\0'.
If you wanted to point to "bc", you might be tempted to do something like...
char p2 = p+1;
However, most C library functions expect you to '\0'-terminate the string, which means that p2 would be pointing to "bcd".
That's why you generally need to copy your substring into a separate character array and then copy a '\0'-terminator into it.
This illustrates the point:
--------8<--------
#include <stdio.h>
#include <string.h>
int main()
{
char *src = "abcd";
char *dst = "01234";
strncpy(dst,src+1,2); /* Copy a substring of two characters from src into dst, starting from the 2nd character in src */
printf("dst is \"%s\"\n",dst);
}
--------8<--------
See the output?
--------8<--------
dst is "bc234"
--------8<--------
NB: you'll get a core dump with that on Linux though, because you are copying data to read-only memory.
Better to write:
--------8<--------
#include <stdio.h>
#include <string.h>
int main()
{
char src[] = "abcd";
char dst[] = "01234";
strncpy(dst,src+1,2);
printf("dst is \"%s\"\n",dst);
}
--------8<--------
That way the src and dst strings are copied into the stack, which you are allowed to write to.
Windows is less discriminating :-)
String in C is simply array of characters terminated by '/0' .
If you are searching for a particular substring you can compare them through indexing.
If you want a substring say , from length 5 to 10 ,
you can start indexing from 5 and go on 10 and every time read it on to another character array.
This is pretty much done in answer above by "Mysidia".
return temp;
would really be
return buffer;
to use this approach you need the offsets, and you need to set aside a buffer, i.e.
int main()
{
char temp[25];
substring_r(temp, 1, 6, "!Hello there...");
printf("%s\n", temp);
}
if you're willing to destroy the original string, then it's even easier..
just null terminate where you want it to stop, and move the start of
the substring to the start of the array.
int main()
{
char temp[25] = "!Hello there...";
memmove(temp, temp+1, 6); /* the 6 is how many bytes to copy starting from the source string
and ending at (temp+1)[5] */
temp[6] = '\0'; /* End the result*/
printf("%s\n", temp);
}
Business Accounts
Answer for Membership
by: Jase-CoderPosted on 2005-01-04 at 03:26:37ID: 12951168
hi try
strchr(char *str, int ch)
for example
#inlucde <string.h>
void main()
{
*p;
p = strchr("This is a test", ' ') // p holds "is a test"
}