我的任务是能够连接两个字符串并使用指针返回一个新字符串。它应该返回一个新字符串,但它当前返回一个空格。这是我的代码:
char* concat(char* first, char* second){
int string1Len = strlen(first);
int string2Len = strlen(second);
int length = string1Len + string2Len;
char* string = malloc(sizeof(char)*length);
int i;
while(i < string1Len){
*(string + i) = *first;
first+= 1;
i+= 1;
}
while( i < length){
*(string + i) = *second;
second+= 1;
i+= 1;
}
return string;
}
答案 0 :(得分:2)
除了单元化int i = 0
之外,你的代码似乎还不错。这适用于我的机器:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
char* concat(char* first, char* second){
int string1Len = strlen(first);
int string2Len = strlen(second);
int length = string1Len + string2Len;
char* string = malloc(sizeof(char)*length + 1);
assert(string);
int i=0;
while(i < string1Len){
string[i] = *first;
first+= 1;
i+= 1;
}
while(i < length){
string[i] = *second;
second += 1;
i += 1;
}
string[i] = '\0';
return string;
}
int main(){
char* x = "foo";
char* y = "bar";
printf("%s\n", concat(x, y));
// prints 'foobar'
}
P.S。使用string[i]
代替*(string + i)
,通常认为它更具可读性。
答案 1 :(得分:1)
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
// You should use char const * as your function don't need to change the character of your input string
char *concat(char const *first, char const *second) { // put * on the variable name not the type
assert(first && second); // test only include for debug build concat expect valid string
size_t first_len = strlen(first); // strlen() return a size_t
size_t second_len = strlen(second);
// you could check for overflow case here
size_t length = first_len + second_len; // malloc take a size_t as argument
char *string = malloc(length + 1); // don't forget the nul terminate byte, sizeof(char) == 1
if (!string) { // don't forget to check malloc() error
return NULL;
}
size_t i = 0; // don't forget to initialize your variable
while (*first) {
string[i++] = *first++; // use [] notation is more readeable in that case
}
while (*second) {
string[i++] = *second++;
}
string[i] = '\0'; // never forget the nul terminate byte
return string;
}
int main(void) {
char *my_amazing_new_string = concat("Stack ", "Overflow");
if (!my_amazing_new_string) {
return EXIT_FAILURE;
}
printf("%s", my_amazing_new_string);
free(my_amazing_new_string);
}
答案 2 :(得分:1)
字符串图书馆?我从不使用它们:P
char * concat(char * dest, const char * src)
{
// creates a pointer to `dest` so we can return it from from its start position.
char * ptr = dest;
// increament dest until the nul-terminated which is 0 (false//loop stops).
while (*dest) dest++;
// so we assign dest pointer to src pointer while increamenting them.
while (*dest++ = *src++);
// return the `dest` pointer.
return ptr;
}
请注意,您需要自己分配第一个参数,因为此函数会连接dest
指针本身,所以返回值(可选 ^^)。
我也注意到由于某种原因......每个人都在功能本身中分配内存,所以
我做了 2nd 版本,但是,它不像第一个版本(尽管你需要返回值)。
char * concat(char * s1, const char * s2)
{
size_t i = 0;
// length of s1.
while (*s1++) i++;
size_t s1_len = i; i = 0;
s1 -= s1_len + 1;
// length of s2.
while (*s2++) i++;
size_t s2_len = i; i = 0;
s2 -= s2_len + 1;
// allocates memory.
char * newStr = (char *)malloc((s1_len + s2_len)+1); // +1 is for the nul-terminated character.
if(!newStr) return newStr; // if malloc fails, return (null) pointer.
// points to position 0 of the 1st string.
char * ptr = newStr;
// copies the content of s1 to the new string
while (*ptr++ = *s1++);
// get back to the nul-terminated to overwrite it.
*ptr--;
// concentrate
while (*ptr++ = *s2++);
// return the `newStr` pointer.
return newStr;
}
答案 3 :(得分:1)
OP的代码错误
// int i;
int i = 0;
// char* string = malloc(sizeof(char)*length);
char* string = malloc(length + 1);
....
*(string + i) ='\0'; // add this
return string;
次要的东西
//char* string = malloc(sizeof(char)*length);
char* string = malloc(length); // Also +1 as noted above
int
可能太窄// int i;
size_t i;
strlen()
&#34;没有字符串库函数&#34;
char* string = malloc(...);
if (string == NULL) { // add
return NULL;
}
const
。这允许更多地使用函数和潜在的优化。
//char* concat(char* first, char* second){
char* concat(const char* first, const char* second){
一些未经测试的替代代码。注意没有整数变量。
char* concat(const char* first, const char* second) {
const char *p1 = first;
while (*p1) {
p1++;
}
const char *p2 = second;
while (*p2) {
p2++;
}
char* string = malloc((p1 - first) + (p2 - second) + 1);
if (string) {
char *ps = string;
while ((*ps = *first++)) {
ps++;
}
while ((*ps = *second++)) {
ps++;
}
}
return string;
}