如何从字符指针One复制到Anothe Char Pointer,并在之间添加字符
我想在c中写下带有句子的char指针的功能,在每个地方都有逗号','我想添加空间' 我写了此功能
char* add_space_after_comma(char* textArr){
char* newText=(char *)malloc(strlen(textArr) * sizeof(textArr));
char* temp = newText;
int i;
int indexNew=0;
int maxSize = strlen(textArr)-1;
if(newText == NULL)
{
printf("memory allocation failed \n");
exit(0);
}
for (i = 0; textArr[i] != '\0'; i++, indexNew++) {
if(indexNew == maxSize-1){
temp = (char*)realloc(newText, maxSize*ARR_SIZE*sizeof(char));
if(temp == NULL)
{
printf("memory allocation failed \n");
exit(0);
}
maxSize= maxSize * ARR_SIZE;
newText =temp;
}
newText[indexNew] = textArr[i];
if(textArr[i] == ','){
indexNew++;
newText[indexNew] = ' ';
}
}
indexNew++;
newText[indexNew] = '\0';
printf("\nthe new text is: %s\n", newText);
return (char*)newText;
}
,但是当我试图运行和检查它时,将其打印到屏幕
新文本是: v。
-
i want to write function in c that gets char pointer with sentence inside, and in every place there is comma ',' i want to add space ' ' after the comma
i write this function
char* add_space_after_comma(char* textArr){
char* newText=(char *)malloc(strlen(textArr) * sizeof(textArr));
char* temp = newText;
int i;
int indexNew=0;
int maxSize = strlen(textArr)-1;
if(newText == NULL)
{
printf("memory allocation failed \n");
exit(0);
}
for (i = 0; textArr[i] != '\0'; i++, indexNew++) {
if(indexNew == maxSize-1){
temp = (char*)realloc(newText, maxSize*ARR_SIZE*sizeof(char));
if(temp == NULL)
{
printf("memory allocation failed \n");
exit(0);
}
maxSize= maxSize * ARR_SIZE;
newText =temp;
}
newText[indexNew] = textArr[i];
if(textArr[i] == ','){
indexNew++;
newText[indexNew] = ' ';
}
}
indexNew++;
newText[indexNew] = '\0';
printf("\nthe new text is: %s\n", newText);
return (char*)newText;
}
but when im trying to run and check it, its print to the screen
the new text is: �����T$
�v���넍�&
how can i write this function rigth?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于初学者,应使用限制符
cosnt
声明函数参数,因为传递的字符串在函数中没有更改。该函数根据源字符串的内容创建一个新字符串。此声明中的内存分配
等同于
,显然没有意义。
内存重新分配效率
低下,也没有意义。首先,您需要计算逗号的发生,然后立即分配具有所需尺寸的数组。
for循环后的索引的这种增加
是错误的。您需要删除此语句。
而且您需要确保通过的指针确实指向字符串。
该函数可以被声明和定义,如下面的演示程序所示。
程序输出是
For starters the function parameter should be declared with the qualifier
cosnt
because the passed string is not changed within the function. The function creates a new string based on the content of the source string.The memory allocation in this declaration
is equivalent to
and evidently does not make a sense.
The memory reallocation
is inefficient and also does not make a sense. You need at first to count occurrences of the comma and then at once to allocate an array with the required size.
This incrementing of the index after the for loop
is wrong. You need to remove this statement.
And you need to be sure that the passed pointer is indeed points to a string.
The function can be declared and defined as shown in the demonstration program below.
The program output is
malloc()
为结果。与一系列realloc()
s相比,字符串的一次解析是有效的。target
字符之后,将函数通用到后缀
char。strlen(ntext)
。-
'(连字符),而不是仅仅''(space):
malloc()
for the result. One time parsing of string is efficient than a series ofrealloc()
s.suffix
a char after any giventarget
character.strlen(nText)
from function if necessary.-
'(hyphen) instead of just '' (space) :