C中不带strcat的字符串连接
在没有strcat库函数的情况下,我很难在C中连接字符串。这是我的密码C中不带strcat的字符串连接,c,string,C,String,在没有strcat库函数的情况下,我很难在C中连接字符串。这是我的密码 #include<stdio.h> #include<string.h> #include<stdlib.h> int main() { char *a1=(char*)malloc(100); strcpy(a1,"Vivek"); char *b1=(char*)malloc(100); strcpy(b1,"Ratnavel"); int i; int le
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
char *a1=(char*)malloc(100);
strcpy(a1,"Vivek");
char *b1=(char*)malloc(100);
strcpy(b1,"Ratnavel");
int i;
int len=strlen(a1);
for(i=0;i<strlen(b1);i++)
{
a1[i+len]=b1[i];
}
a1[i+len]='\0';
printf("\n\n A: %s",a1);
return 0;
}
#包括
#包括
#包括
int main()
{
char*a1=(char*)malloc(100);
strcpy(a1,“Vivek”);
char*b1=(char*)malloc(100);
strcpy(b1,“鼠脐”);
int i;
int len=strlen(a1);
对于(i=0;i您无法安全地写入这些数组,因为您无法确保有足够的可用空间。如果使用malloc()
分配空间,则无法通过分配字符串文字来覆盖指针。在这种情况下,您需要使用strcpy()
将字符串复制到新分配的缓冲区中
另外,C中字符串的长度是由strlen()
函数计算的,而不是您正在使用的length()
连接时,您需要在适当的位置终止,而您的代码似乎没有这样做
如果出于某种原因需要,我将如何重新实现strcat()
:
char * my_strcat(char *out, const char *in)
{
char *anchor = out;
size_t olen;
if(out == NULL || in == NULL)
return NULL;
olen = strlen(out);
out += olen;
while(*out++ = *in++)
;
return anchor;
}
请注意,当涉及缓冲区溢出时,这与strcat()一样糟糕,因为它不支持限制输出中使用的空间,它只是假设有足够的可用空间
char a1[] = "Vivek";
将创建一个大小为6
的字符数组a1
。您正试图用超出其容纳范围的字符填充它
如果您希望能够容纳串联“Vivek”
和“Ratnavel”
您需要一个大小至少为14(5+8+1)
的字符数组
在您的修改的程序中,您正在执行:
char *a1=(char*)malloc(100); // 1
a1 = "Vivek"; // 2
1:将分配一个大小为100字节的内存块,使a1
指向它。2:将使
a1
指向字符串文本“Vivek”
。此字符串文本无法修改
要解决此问题,请使用strcpy将字符串复制到分配的内存中:
char *a1=(char*)malloc(100);
strcpy(a1,"Vivek");
还有循环条件i问题的:
length
不是一个函数。strlen
是,但你可能不应该在循环中调用它-b1
的长度在我们身上不会改变,是吗?此外,它返回一个size\u t
,它可能与你平台上的int
大小相同,但将是未签名的。这可以(但通常不会)导致错误,但无论如何你都应该做对
a1
只为第一个字符串分配了足够的空间,因为编译器不知道如何为字符串的其余部分分配额外的堆栈空间。如果您提供了明确的大小,如[100]
,这应该足以满足您的目的。如果您需要健壮的代码,而不假设什么是“足够的”,你应该看看malloc和朋友,尽管这可能是以后的教训
您的循环停止得太早。i
(假设您有一个变量,b1_len
,在循环开始之前设置为b1
)就足够了-strlen
不计算'\0'
但是说到最后计算'\0'
,一个稍微高效的实现可以使用a1-1的大小,而不是本例中的strlen(a1)
,因为a1
(和b1
)声明为数组,而不是指针。这是您的选择,但请记住sizeof
不适用于指针,因此不要将它们混淆。
编辑:新问题:
char*p=malloc(/*some*/);p=/*something*/
是一个问题。=
带有指针的指针不会复制内容,而是复制值,因此您丢弃了从malloc
获得的旧指针值。要将字符串内容复制到char*
(或char[]
)您需要使用strcpy
,strncpy
,或者(我的首选)memcpy
(或者只是一个循环,但这相当愚蠢。同样,如果您正在编写自己的strcat
,这可能是一个很好的做法)
除非你使用C++,否则我不会把返回值“代码> MalOC/<代码>,但是这是一个宗教战争,我们不需要其中的一个。
如果您有strdup
,请使用它。如果您没有,下面是一个有效的实现:
char *strdup(const char *c)
{
size_t l = strlen(c);
char *d = malloc(l + 1);
if(d) memcpy(d, c, l + 1);
return d;
}
它是C标准库中没有的最有用的函数之一
最好将strcat
逻辑分解为单独的函数。如果使用指针算法,则不需要strlen
函数:
#include <stdio.h>
#include <stdlib.h>
#include <string.h> /* To completely get rid of this,
implement your our strcpy as well */
static void
my_strcat (char* dest, char* src)
{
while (*dest) ++dest;
while (*src) *(dest++) = *(src++);
*dest = 0;
}
int
main()
{
char* a1 = malloc(100);
char* b1 = malloc(100);
strcpy (a1, "Vivek");
strcpy (b1, " Ratnavel");
my_strcat (a1, b1);
printf ("%s\n", a1); /* => Vivek Ratnavel */
free (a1);
free (b1);
return 0;
}
#包括
#包括
#包括/*以完全消除此问题,
实现您的strcpy以及我们的strcpy*/
静态空隙
my_strcat(char*dest,char*src)
{
而(*dest)+dest;
而(*src)*(dest++)=*(src++);
*dest=0;
}
int
main()
{
char*a1=malloc(100);
char*b1=malloc(100);
strcpy(a1,“Vivek”);
strcpy(b1,“鼠脐”);
我的strcat(a1、b1);
printf(“%s\n”,a1);/*=>Vivek Ratnavel*/
免费(a1);
免费(b1);
返回0;
}
下面是旧答案
可以像在代码中一样,使用strcpy初始化字符串,也可以在声明字符数组时直接初始化字符串
char a1[100] = "Vivek";
除此之外,你可以一个字符一个字符地做
a1[0] = 'V';
a1[1] = 'i';
// ...
a1[4] = 'k';
a1[5] = '\0';
或者,您可以编写几行代码来替换strcpy,并将其作为函数或直接用于主函数
旧答案
你有
0 1 2 3 4 5 6 7 8 9 ...
a1 [V|i|v|e|k|0|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_]
b1 [R|a|t|n|a|v|e|l|0|_|_|_|_|_|_|_|_|_|_|_|_|_]
但是有循环之类的,对吗
尝试此操作(请记住添加缺少的位)
for(aindex=5;aindex<14;aindex++){
a1[aindex]=b1[aindex-5];
}
现在想想上面循环中的5
和14
你可以用什么来代替它们呢?当你回答这个问题时,你已经解决了你的编程问题:)你也可以使用strcpy()来完成它;)
我认为这是一个简单的问题
#include<stdio.h>
int xstrlen(char *);
void xstrcat(char *,char *,int);
void main()
{
char source[]="Sarker";
char target[30]="Maruf";
int j=xstrlen(target);
xstrcat(target,source,j);
printf("Source String: %s\nTarget String: %s",source,target);
}
int xstrlen(char *s)
{
int len=0;
while(*s!='\0')
{
len++;
s++;
}
return len;
}
void xstrcat(char *t,char *s,int j)
{
while(*t!='\0')
{
*t=*t;
t++;
}
while(*s!='\0')
{
*t=*s;
s++;
t++;
}
}
#包括
int-xstrlen(char*);
void xstrcat(char*,char*,int);
void main()
{
字符源[]=“萨克”;
C
0 1 2 3 4 5 6 7 8 9 ...
a1 [V|i|v|e|k|0|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_|_]
b1 [R|a|t|n|a|v|e|l|0|_|_|_|_|_|_|_|_|_|_|_|_|_]
0 1 2 3 4 5 6 7 8 9 ...
a1 [V|i|v|e|k|R|a|t|n|a|v|e|l|0|_|_|_|_|_|_|_|_]
a1[5] = 'R';
a1[6] = 'a';
// ...
a1[12] = 'l';
a1[13] = '\0';
for (aindex = 5; aindex < 14; aindex++) {
a1[aindex] = b1[aindex - 5];
}
char *a = (char *) malloc(100);
char *b = (char *) malloc(100);
strcpy(a, "abc"); // initializes a
strcpy(b, "def"); // and b
strcpy((a + strlen(a)), b); // copy b at end of a
printf("%s\n",a); // will produce: "abcdef"
#include<stdio.h>
int xstrlen(char *);
void xstrcat(char *,char *,int);
void main()
{
char source[]="Sarker";
char target[30]="Maruf";
int j=xstrlen(target);
xstrcat(target,source,j);
printf("Source String: %s\nTarget String: %s",source,target);
}
int xstrlen(char *s)
{
int len=0;
while(*s!='\0')
{
len++;
s++;
}
return len;
}
void xstrcat(char *t,char *s,int j)
{
while(*t!='\0')
{
*t=*t;
t++;
}
while(*s!='\0')
{
*t=*s;
s++;
t++;
}
}