字符串的连接
//实现两个串的连接#include<stdio.h>
#include<stdlib.h>
typedef struct
{
char *ch;
int len;
}Hstring;
void concatstring(Hstring s,Hstring p,Hstring q)
{
int i;
s.ch=malloc((p.len+q.len)*sizeof(char));//根据所需容量申请空间
for(i=0;i<p.len;i++)
s.ch[i]=p.ch[i];
for(i=0;i<q.len;i++)
s.ch[i+p.len]=q.ch[i];
s.len=p.len+q.len;
puts(s.ch);
}
int main(void)
{
int i;
Hstring s,s1,s2;
s1.len=0;
s2.len=0;
printf("请输入第一个字符串s1:\n");
scanf("%s",s1.ch);
printf("请输入第二个字符串s2:\n");
scanf("%s",s2.ch);
for(i=0;s1.ch[i]!='\0';i++)
s1.len++;
for(i=0;s2.ch[i]!='\0';i++)
s2.len++;
concatstring(s,s1,s2);
}
C:\Users\Administrator\Desktop\c21.c(27) : warning C4700: local variable 's1' used without having been initialized
C:\Users\Administrator\Desktop\c21.c(29) : warning C4700: local variable 's2' used without having been initialized
C:\Users\Administrator\Desktop\c21.c(34) : warning C4700: local variable 's' used without having been initialized