Languages :: General :: C Substring Function |
|||
| By: progGoon |
Date: 26/04/2003 00:00:00 |
Points: 75 | Status: Answered Quality : Excellent |
|
I've been trying to do this code and it seem not to work and I need your help. This is in C programming only not C++. I need to write a function for substring with four parameters: str, substr, i and len. The parameters str and substr are char arrays. The outcome will be: Enter a string: The cat sat on the mat i is 8 len is 3 the string will be sat |
|||
| By: VGR | Date: 27/04/2003 05:36:00 | Type : Answer |
|
| tips : // tokenize on space delimitor may help, a single for loop would do. // else, the dirty way : result=""; i=strpos(str,substr); if (i) { j=strpos(substr(str,i)," "); // right part j=j+i; // real position in first string result=substr(str,i,j-i+1); // in case you need it, length is strlen(result); } return result; |
|||
| By: FiatLink | Date: 27/04/2003 19:08:00 | Type : Comment |
|
| <A HREF="http://home.work.not/done/here.html">http://home.work.not/done/here.html</a> please post code what you have done so far |
|||
| By: collegeBoy | Date: 27/04/2003 19:32:00 | Type : Assist |
|
| void substr(char* str, char* substr, int start, int len) { strncpy(substr, str + start, len); substr[len] = '\0'; } |
|||
| By: progGoon | Date: 03/05/2003 15:44:00 | Type : Comment |
|
| Write a function substring with four parameters: str, substr, i and len. The parameters str and substr are char arrays. The parameters i and len are ints. The function substring normally copies len consecutive characters from str, beginning at index i, into substr. If the null terminator is encountered in str, the function will stop copying characters into substr. Assume that substr is large enough to hold the characters copied and the null terminator. Example: The string is: The cat sat on the mat i is 8 len is 3 after running function, substr will be: sat |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








