inserting a substring into another string in c

不羁岁月 提交于 2019-12-13 08:04:37

问题


i just started learning c. i am doing an exercise and the question is as follows.

Write a function called insertString to insert one character string into another string.The arguments to the function should consist of the source string, the string to be inserted, and the position in the source string where the string is to be inserted. So, the call insertString (text, "per", 10); with text as originally defined "the wrong son" results in the character string "per" being inserted inside text, beginning at text[10].Therefore, the character string "the wrong person" is stored inside the text array after the function returned.

#include<stdio.h>
int insertString(char[],char[],int);
int stringLength(char[]);
int main()
{
  char text[]="the wrong son";
  int result=insertString(text,"per",10);
  if(result!=-1)
    printf("string 1 is : %s \n",text);
  else
    printf("Not possible\n");
  return 0;
}
int insertString(char a[],char b[],int pos)
{
  int i=0,j=0;
  int lengthA=stringLength(a);
  int lengthB=stringLength(b);
  if(pos>lengthA)
    return -1;

  for(i=lengthA;i>=pos;i--)
      a[i+lengthB]=a[i];

  for ( i = 0; i < lengthB; ++i )
      a[i + pos] = b[i];

  return 1;
}
int stringLength(char x[])
{
  int length=0;
  while(x[length]!='\0')
    length++;
  return length;
}

i have done this and it's working too. but i am receiving a message abort trap : 6. when i looked upon it, i learned it's an error because i am writing to the memory that i don't own. since i have used variable length character arrays, wherever the null character is, indicates the end of array and i am trying to extending it by inserting a string, that's my understanding. am i right so far?

i am also moving the null character. i don't know whether it's right or wrong.

so is there a way to get around this error? Also, i don't know pointers yet and they're in the next chapter of the textbook .

Any help in this would be appreciated very much.


回答1:


A variable-length array is a very specific C construct that has nothing to do with what your textbook calls "variable length arrays". If I were you I would not trust this textbook if it said that 1+1=2. So much for it.

A character array that ends with a null character is called string by pretty much everyone, everywhere.

char text[]="the wrong son";

Your textbook led you to believe that text will hold as many characters as you need. Alas, there is no such thing in C. In fact text will hold exactly as many characters as there are in its initializer, plus 1 for the null terminator, so you cannot insert anything in it.

In order for your program to work, you need to explicitly allocate as many characters for text as the resulting string will contain.

So as there are 14 characters in "the wrong son" (including the terminator) and three characters in "per" (not including the terminator), you need 17 characters in total:

char text[17]="the wrong son";

You can also check your calculations:

int result=insertString(text, "per", 10, sizeof(text));
...
int insertString(char a[], char b[], int pos, int capacity)
{
   ...
   if (lengthA + lengthB + 1 < capacity) 
     return -1;
   ...


来源:https://stackoverflow.com/questions/36629795/inserting-a-substring-into-another-string-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!