GeekInterview.com
  I am new, Sign me up!
 
Home C
 

Position of Substrings

 
Category: C
Comments (4)


How to determine different positions of substrings within a string in C?


There are numerous functions available in C to achieve the purpose of finding different positions of a substring with a string.


strstr():


To obtain the first occurrence of substring in a string the function used is strstr().


The syntax for this function is


char *strstr( const char *str1, const char *str2 );


The header file that must be included while using this function strstr() is <string.h>


In the above the function strstr() returns a pointer to the first occurrence of str2 in str1. If no match is found then NULL is returned.


strrstr():


This function is used to obtain the last occurrence of substring in a string.


The syntax for this function is


strrstr(const char *s, const char *subs );


The header file that must be included while using the function strrstr() is <sdgstd.h>


This returns a pointer which points to the last occurrence of a substring within another string. If the substring is not found, this will be the NULL pointer.



Read Next: printf() Function Return Value



 
Related Topics


 

Comments


payel said:

  How to get a substring if the positions are indicated? For example, if I want to extract "viv" from the word "vivek"?
February 8, 2008, 11:40 am

rajesh2kx said:

  Try assigning the string to a pointer and try the pointer traversal to get the string with desired position
September 9, 2008, 7:06 am

sumitsolution said:

  #include
#include
int main(){
char *s="vivek";
char *t;
int i;
clrscr()
for(i=0;i<3;i++){
t[i]=s[i];
}
t[i]='n';
printf("%s",t);
getch();
return 0;
}
October 8, 2009, 1:40 pm

sumitsolution said:

  #include
#include
int main(){
char *s="vivek";
char *t;
int i;
clrscr()
for(i=0;i<3;i++){
t[i]=s[i];
}
t[i]='n';
printf("%s",t);
getch();
return 0;
}
October 8, 2009, 1:45 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact  

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape