[英]Using concat in C without string library functions or array notation
我的任務是能夠連接兩個字符串並使用指針返回一個新字符串。 它應該返回一個新字符串,但它當前返回一個空格。 這是我的代碼:
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;
}
除了unitialized 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'
}
PS使用string[i]
而不是*(string + i)
,它通常被認為更具可讀性。
#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);
}
字符串庫? 我從不使用它們: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
指針本身,因此返回值( 可選 ^^)。
我也注意到由於某種原因......每個人都在函數本身中分配內存,所以
我做了我的第二個版本,但是,它不像第一個版本(你需要返回值)。
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;
}
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()
“沒有字符串庫函數”
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.