| CODE |
#include <iostream> using std::cout; using std::cin; using std::endl; int InsertStr(char *dst, const char *src, const char *str, int pos) { int i = 0; while(*src) { if (i == pos) { while (*str) { *dst = *str; ++dst; ++str; ++i; } } else { *dst = *src; ++dst; ++src; ++i; } } *dst++ = '\0'; return i; // how many chars are in the buffer } int main() { char s[64]; cout << InsertStr(s, "Hello people", " C++", 5) << endl; cout << s << endl; cin.get(); return 0; } |
| CODE |
/* * InsertStr: * Arguments: * OUT: dst: Assumed to point at a buffer of at least strlen(src) + strlen(str) + 1 * IN: src: A null terminated string. * IN: str: A null terminated string. * IN: pos: The position in dst that 'str' will be inserted. * Description: * If pos <= strlen(src) then str will be inserted into src starting at pos. * The resulting string copied to dst * else src will copied to dst. */ |
| CODE |
| /* * Method 1: Using loops. * Obvious but not the most effecient. */ int InsertStr1(char*dst,const char* src,const char* str,int pos) { int loop; for(int loop=0;(loop < pos) && ((*src) != '\0');++loop,++dst,++src) { (*dst) = (*src); } /* * Add this condition to make sure it behaves like the orginal code * posted by Joel */ if (loop == pos) { for(;(*str) != '\0';++str,++dst) { (*dst) = (*str); } for(;(*src) != '\0';++dst,++src) { (*dst) = (*src); } } } |
| CODE |
| /* * Method 2: Using strcpy * Use the standard C string handling functions. * This may be faster than method 1 as these libraries are often optimized. * Also this is easier to read. */ int InsertStr2(char*dst,const char* src,const char* str,size_t pos) { int len = std::min(pos,strlen(src)); strncpy(dst,src,len); dst[len] = '\0'; // Make sure it is null terminated. if (pos > strlen(src) then it may not be if (len == pos) { strcat(dst,str); strcat(dst,src+len); } } |
| QUOTE |
What happens if pos > strlen(src)? |
| QUOTE (dr voodoo @ Jan 2 2006, 05:57 AM) |
| @myork you did not document that dst and src must be different |
| QUOTE (dr voodoo @ Jan 2 2006, 05:57 AM) |
| Also you declare your function as returning int but never return anything. |
| QUOTE |
| Remember when sombody uses your functions they may not have access to the code (or the skill to understand it). |
| CODE |
#include <iostream> using std::cout; using std::cin; using std::endl; /* _strlen: Emulator for strlen Params: IN: str returns the number of chars. of 'str' */ int _strlen(const char *str) { int i = 0; for(;(*str) != '\0'; ++i, ++str){} return i; } /* InsertStr: Function to insert the first 'len' chars of 'str' on 'src' starting position 'pos' Params: IN: src, str, pos, len OUT: dst Notes: 1. if pos > 'str' then 'src' will be concatened with 'str'. 2. if len > 'src' or len <= 0 then 'str' will be used complety returns the number of chars on 'dst' */ int InsertStr(char *dst, const char *src, const char *str, int pos, int len) { int j = 0; int l = _strlen(src); // if 'pos' is bigger than 'src' if (pos >= l) { // concatenate 'str' with 'src' into 'dst' for(l = 0;*dst = *src; ++l, ++dst, ++src) {} for(;*dst = *str; ++l, ++dst, ++str) {} // ads null termination *dst++ = '\0'; return l; } // number of chars on 'str' l = _strlen(str); // if it isn't, start the insertion... for(;*src != '\0'; ++j, ++src, ++dst) { // if our counter matches the 'pos' if (j == pos) { // begin 'str' insertion... // let's see if 'len' is valid number if ( (len <= 0) || (len >= l) ) { // if len <= 0 than 'str' or // len > 'str' // 'str' will be inserted complety for(;*str != '\0'; ++j, ++str, ++dst) { *dst = *str; } } else { // let's insert the first 'len' of 'str' on 'src' for(;(*str != '\0') && (len != 0); ++j, --len, ++str, ++dst) { *dst = *str; } } } // continues copying into buffer *dst = *src; } // ads null termination *dst++ = '\0'; return j; } int main() { char s[64]; cout << InsertStr(s, "JoelPower", "-void", 4, 1) << endl; cout << "[" << s << "]" << endl; cin.get(); return 0; } |