本文介绍了使用指针连接两个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 使用指针连接两个字符串,它无法正常工作。请帮助我.......... #include< iostream> 使用 命名空间标准; int main() { char * str1 = krishna; char * str2 = Prasad; char * str3; int count = 0 ; int i,j; for (i = 0 ; * str1!= NULL; i ++,* str1 ++) { count ++; } for (j = 0 ; * str2!= NULL; j ++ ,* str2 ++) { count ++; } // str3 = new char [sizeof(str1)+ sizeof(str2)] ; str3 =(char *)malloc( 13 ); for (i = 0 ; str1 [i] != NULL; i ++) { str3 [i] = str1 [i]; } for (j = 0 ; str2 [j ]!= NULL; j ++,i ++) { str3 [i] = str2 [j]; } cout<< endl; cout<< 字符串3 =; while (* str3) { cout<< * str3 ++; } return 0 ; } 解决方案 当使用malloc或new为nul终止字符串分配字符数组时,需要包括一个额外的字符用于终止nul。 替换它: str3 =( char *)malloc( 13 ); 这个: str3 =( char *)malloc(count + 1 ); 或者这个: str3 = new char [count + 1 ]; 不要忘记用空字符终止新字符串。 替换为: for (j = 0 ; str2 [j]!= NULL; j ++,i ++) { str3 [i] = str2 [j]; } 这个: for (j = 0 ; str2 [j]!= 0 ; j ++,i ++) { str3 [i] = str2 [j]; } str3 [i] = 0 ; 既然新字符串已经终止,你可以替换它: while (* str3) { cout<< * str3 ++; } 这个: cout<< str3; 以下代码告诉我你尝试了一些东西,但它不起作用。 // str3 = new char [sizeof(str1)+ sizeof (str2)]; 原因是sizeof运算符没有给出字符串的长度但是大小字符串指针。 正如建议你应该正确处理字符串终止符。此外,您应该处理可能失败的调用 malloc 并正确释放动态分配的内存。 #include < stdio.h > #include < stdlib.h > int my_strlen( const char * s) { int l = 0 ; while (s [l])++ l; return l; } int main() { const char * s1 = krishna ; const char * s2 = Prasad; int l1 = my_strlen(s1); int l2 = my_strlen(s2); char * s3 =( char *)malloc(l1 + l2 + 1 ); // 为字符串终结符腾出空间 if (!s3) return - 1 ; // malloc失败 int n1,n2,n3; n3 = 0 ; for (n1 = 0 ; n1< l1; ++ n1,++ n3 ) s3 [n3] = s1 [n1]; for (n2 = 0 ; n2< l2; ++ n2 ,++ n3) s3 [n3] = s2 [n2]; s3 [n3] = ' \ 0'; // 追加字符串终结符 printf( string 3是%s \ n,s3); 免费(s3); // 释放已分配的内存 return 0 ; } 很抱歉,但是当我看到这样的代码时我无法抗拒:)。 C语言是基本的,不值得重新发明轮子。 使用标准例程并按原样编码: int main() { char * str1 = krishna; char * str2 = Prasad; char * str3; str3 = malloc(strlen(str1)+ strlen(str2)+ 1 ); // 为2个字符串的长度加上最终的空值分配空格。 strcpy(str3,str1); // 复制结果中的第一个字符串 strcat(str3,str2): // 附加第二个字符串。 cout<< STR3; // 打印全新字符串! 返回 0 ; } Hi,CONCATENATION OF TWO STRINGS USING POINTER, it is not working. pls help me..........#include<iostream>using namespace std;int main(){ char *str1 = "krishna"; char *str2 = "Prasad"; char *str3; int count = 0; int i,j; for(i=0;*str1!=NULL;i++,*str1++) { count++; } for(j= 0; *str2!=NULL;j++,*str2++) { count++; } //str3 = new char[sizeof(str1)+sizeof(str2)]; str3 = (char*) malloc(13); for(i=0;str1[i]!=NULL;i++) { str3[i] = str1[i]; } for(j= 0; str2[j]!=NULL;j++,i++) { str3[i] = str2[j]; } cout<<endl; cout<<"String 3 is = "; while(*str3) { cout<<*str3++; } return 0;} 解决方案 When using malloc or new to allocate an array of characters for a nul terminated string, you need to include one extra character for the terminating nul.Replace this:str3 = (char*) malloc(13);with this:str3 = (char*) malloc(count + 1);or this:str3 = new char[count + 1];Don't forget to terminate the new string with a nul character.Replace this:for(j= 0; str2[j]!=NULL;j++,i++){ str3[i] = str2[j];}with this:for(j=0; str2[j]!=0; j++,i++){ str3[i] = str2[j];}str3[i] = 0;Now that the new string is nul terminated, you can replace this:while(*str3){ cout<<*str3++;}with this:cout << str3;The following code tells me you tried something but it did not work.//str3 = new char[sizeof(str1)+sizeof(str2)];The reason is the sizeof operator does not give you the length of a string but the size of the string pointer.As suggested you should properly handle string terminators. Moreover, you should handle a possibly failing call to malloc and properly release dynamically allocated memory. #include <stdio.h> #include <stdlib.h> int my_strlen(const char * s) { int l = 0; while ( s[l] ) ++l; return l; } int main() { const char *s1 = "krishna"; const char *s2 = "Prasad"; int l1 = my_strlen(s1); int l2 = my_strlen(s2); char * s3 = (char *) malloc(l1 + l2 + 1); // make room for the string terminator if ( ! s3 ) return -1; // malloc failure int n1, n2, n3; n3 = 0; for (n1=0; n1 < l1; ++n1, ++n3) s3[n3] = s1[n1]; for (n2=0; n2 < l2; ++n2, ++n3) s3[n3] = s2[n2]; s3[n3] = '\0'; // append the string terminator printf("string 3 is %s\n", s3); free(s3); // release allocated memory return 0;}Sorry, but when I see code like that I can't resist :).C language is basic enough to don't be worth to reinvent the wheel.Use standard routines and code it as it should be:int main(){ char *str1 = "krishna"; char *str2 = "Prasad"; char *str3; str3 = malloc(strlen(str1) + strlen(str2) + 1); //Allocate space for the sum of the lenghts of the 2 strings plus the final null. strcpy(str3, str1); //Copy first string in the result strcat(str3, str2): //Append the second string. cout << str3; //print whole new string! return 0;} 这篇关于使用指针连接两个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-24 18:08