合并字符串

题目


合并两个字符串,每个字符串长度不小于1不超过50,

主函数已经给出,在join.h头文件中完成join函数,函数原型如下:

char* join(char* a, int alength, char* b, int blength)

需要在join函数中动态申请内存,长度为a和b长度之和加1(因为字符串结尾有‘\0’); 函数返回值即所动态申请内存的首地址。

 

输入:两个字符串,每个一行

输出:合并后的字符串及所申请内存的实际大小,字符串一行,实际大小一行

 

Sample:

input:

1234

567890

output:

1234567890

24


Hint:

使用malloc函数进行动态内存申请,申请到的实际内存大小与操作系统相关,大部分情况下不等于所申请的具体数值。


//join.h
#include<stdio.h>
char* join(char* a, int alength, char* b, int blength) {
  int i;
  char ch;
  char* c = malloc(sizeof(ch) * (alength+blength+1));
  for (i = 0; i < alength; i++)
      c[i] = a[i];
  for (i = alength; i < alength+blength+1; i++)
      c[i] = b[i-alength];
  return c;
}
//main.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
#include"join.h"
#define MAX 51
int main() {
    char a[MAX];
    char b[MAX];
    char* c = NULL;
    scanf("%s", a);
    scanf("%s", b);
    c = join(a, strlen(a), b, strlen(b));
    printf("%s\n", c);
    printf("%d\n", malloc_usable_size(c));
    free(c);
    return 0;
}

笔记:


1、google style出了两次问题,一个是强制类型转换,c = (char*)...不能通过;还有一个新的强制转换类型,没有使用过,另一个就是sizeof()里面必须是变量名,不要加类型;

2、malloc分配的内存为alength+blength+1;若输出为字符串数组则不用加'\0',若为字符,只需令c[alength+blength+1] = '\0'

3、strlen得到的值不包括'\0'

4、有关标准答案的memcpy函数总结

#ifndef __JOINS__
#define __JOINS__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>
char* join(char* a, int alength, char* b, int blength) {
    char* c = malloc(sizeof(a[0]) * (alength + blength + 1));
    memcpy(c, a, sizeof(a[0]) * alength);
    memcpy(c + alength, b, sizeof(b[0]) * blength);
    c[alength + blength] = '\0';
    return c;
}
#endif

 

memcpy函数:c和c++使用的内存拷贝函数,memcpy函数的功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中,返回dest的值。

函数原型:void *memcpy(void *dest, const void *src, size_t n);

所需头文件:#include <string.h>

具体说明:

1.source和destin所指的内存区域可以重叠,但是如果source和destin所指的内存区域重叠,那么这个函数并不能够确保source所在重叠区域在拷贝之前被覆盖。而使用memmove可以用来处理重叠区域。函数返回指向destin的指针。

2.strcpy和memcpy主要有以下3方面的奇。

2.1、复制的内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组、整型、结构体、类等。

2.2、复制的方法不同。strcpy不需要指定长度,它遇到被复制字符的串结束符"\0"才结束,所以容易溢出。memcpy则是根据其第3个参数决定复制的长度。

2.3、用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy

3.如果目标数组destin本身已有数据,执行memcpy()后,将覆盖原有数据(最多覆盖n)。如果要追加数据,则每次执行memcpy后,要将目标数组地址增加到你要追加数据的地址。

注意:source和destin都不一定是数组,任意的可读写的空间均可。memcpy的实现过程,里面是一个一个字符的赋值的,想连续赋值,就要把指针指向连续的内存的首地址。由于函数拷贝的过程是一个字节一个字节的拷贝的,所以实际操作的时候要把void*强制转化为char*,这样在指针加的时候才会保证每次加一个字节。

example1

作用:将s中的字符串复制到字符数组d中。

// memcpy.c

#include <stdio.h>

#include <string.h>

int main()

{

char *s="Golden Global View";

char d[20];

clrscr();

memcpy(d,s,(strlen(s)+1));

printf("%s",d);

getchar();

return 0;

}

输出结果:Golden Global View

example2

作用:将s中第14个字符开始的4个连续字符复制到d中。(从0开始)

#include <string.h>

int main()

{

char *s="Golden Global View";

char d[20];

memcpy(d,s+14,4); //从第14个字符(V)开始复制,连续复制4个字符(View)

//memcpy(d,s+14*sizeof(char),4*sizeof(char));也可

d[4]='\0';

printf("%s",d);

getchar();

return 0;

}

输出结果: View

example3

作用:复制后覆盖原有部分数据

#include <stdio.h>

#include <string.h>

int main(void)

{

char src[] = "******************************";

char dest[] = "abcdefghijlkmnopqrstuvwxyz0123as6";

printf("destination before memcpy: %s\n", dest);

memcpy(dest, src, strlen(src));

printf("destination after memcpy: %s\n", dest);

return 0;

}

输出结果:

destination before memcpy:abcdefghijlkmnopqrstuvwxyz0123as6

destination after memcpy: ******************************as6

posted on 2015-12-13 15:10  pora  阅读(571)  评论(0编辑  收藏  举报

导航