我试图创建一个函数,可以将长度为len的char数组插入到字符串类型的给定索引中。但是,它的行为方式不应该。
当尝试在初始数组的末尾插入时,buf
的内容将以相反的顺序插入字符串的开头。
即。将world
插入Hello
会变为dlrowHello
当尝试在初始案例的中间插入时,输出为worldHello
str->bytes.data
是要修改的数组。
str->bytes.usage
是该数组的长度。
bool string_insert(string_t * const str, size_t index,
const char * const buf, size_t len)
{
bool success = false;
uint8_t orig_size = str->bytes.usage;
uint8_t * temp;
if (str->len < index){
return false;
}
if(str->bytes.usage+len>=str->bytes.usage){
temp = malloc(sizeof(char) * (str->bytes.usage + len));
}
else {
temp = malloc(sizeof(char) * (str->bytes.usage));
}
if (temp == NULL){
success = false;
}
else{
if (index == 0){ //inserts at beginning
for (int k = 0; k < len; k++){
temp[k] = buf[k];
}
for (int j = len; j < str->bytes.usage+len; j++){
temp[j] = str->bytes.data[j - len];
}
}
else if (index == str->bytes.usage){ //inserts at end
for (int h = 0; h < str->bytes.usage; h++){
temp[h] = str->bytes.data[h];
}
for (int g = 0; g < len; g++){
temp[g+str->bytes.usage] = buf[g];
}
}
else{ //inserts in the middle
for (int i = 0; i < index; i++){
temp[i] = str->bytes.data[i];
}
for(int i = index; i < index + len; i++){
temp[i] = buf[i-index];
}
for(int i = index + len; i < str->bytes.usage+len; i++){
temp[i] = str->bytes.data[i-len];
}
}
string_free(str);
str->bytes.data = temp;
str->bytes.dim = 2*str->bytes.usage;
str->bytes.usage = orig_size+len;
success = true;
}
return success;
}
基本上,我只需要知道我是否失明并且遗漏了一些明显的东西。
答案 0 :(得分:0)
当index为0时,它的顺序不相反?
if (index == 0){ //inserts at beginning of array
for (int k = 0; k < len; k++){
temp[k] = buf[k];
更改为
if (index == 0){ //inserts at beginning of array
for (int k = 0; k < len; k++){
temp[k] = buf[len - k - 1];
答案 1 :(得分:0)
// as i understand it:
// str->bytes.data = allocated memory
// str->bytes.dim = size of the allocated memory
// str->bytes.usage= size of the used memory
//
#define BLOCK_SIZE 32
bool string_insert(string_t * const str, size_t index,
const char * buf, size_t len)
{
int max_size;
int i;
char *start;
char *end;
char *tmp=str->bytes.data;
max_size=str->bytes.usage+len;
if(max_size>=str->bytes.dim){
// realloc by BLOCK_SIZE steps
max_size=((max_size/BLOCK_SIZE)+1)*BLOCK_SIZE;
tmp=realloc(tmp,max_size);
if(!tmp) return 0; //false
//update fields with new values
str->bytes.data=tmp;
str->bytes.dim=max_size;
}
start=tmp+index;
end=tmp+(str->bytes.usage);
//shift content after the insertion point
while(end>start){
end--;
*(end+len)=*end;
}
// no comments
strncpy (start,buf,len);
str->bytes.usage+=len;
return 1;
}
答案 2 :(得分:0)
您可以尝试与此相似的内容
,而不是发布的回合假设:
string_t定义为:
typedef结构 { char * pStr; int strLen; } string_t;
string_t *string_insert(
const string_t * originalStr,
const string_t * strToInsert,
size_t index )
{
string_t *newString = NULL;
if( NULL != (newString = malloc( sizeof( string_t )) ) )
{ // then malloc successful
newString->pStr = NULL;
newString->strLen = 0;
// allocate enough room.
if(NULL != (newString->pStr =
malloc( originalStr->strlen + strToInsert->strlen )
{// then mallooc successful
newString->strLen =
origianlStr->strlen + strToInsert->strLen;
//copy first part of originalStr to newString
for( int i = 0; i < index; i++ )
{
newString->pStr[i] = originalStr->pStr[i];
}
// copy string to insert
for( int j=0; j<strToInsert->strlen; j++ )
{
newStr->pStr[i+j] = strToInsert-pStr[j];
}
// copy last part of originalStr to newStr
for ( int k = 0; k<newString->strLen; k++ )
{
newStr->pStr[i+j+k] = origianalStr->pStr[index+k];
}
}
else
{
free( newString );
newString = NULL;
}
}
return newString
} // end function: string_insert
答案 3 :(得分:0)
你有
if (str->len < index){
,但您未能为str->len
分配任何内容。