//用向量 #include<iostream> (720)#include<vector> #define VEC vector<char> using namespace std; int main() { VEC str1,str2; VEC::iterator ite; char ch; bool flag = false; while(cin.get(ch)) { if(ch == ' ') { flag = true; continue; } if(ch != '\n') { if(!flag) str1.push_back(ch); else str2.push_back(ch); } else { str1.insert(str1.end(), str2.begin(), str2.end()); str2.clear(); for(ite = str1.begin(); ite != str1.end(); ite++) cout<<*ite; cout<<endl; str1.clear(); } } return 0; }
/*实现功能:不借用任何字符串库函数,实现无冗余地接收两个字符串,然后把 它们无冗余的连接起来 */ #include<stdio.h> #include<stdlib.h> int main() { int i=1, j=1, k; char a; char *p1 = (char *)malloc(sizeof(char)); char *p2 = (char *)malloc(sizeof(char)); while( (a=getchar() )!='/n') //无冗余地输入字符串1 { p1 =(char *) realloc(p1, sizeof(char)*i ); *(p1+i-1) = a; i++; } p1 = (char *)realloc(p1, sizeof(char)*i); //为字符串结束标志多开辟一个字节的空间 *(p1+i-1) = '/0'; //添加字符串结束标志 while( (a=getchar() )!='/n') //无冗余地输入字符串2 { p2 =(char *) realloc(p2, sizeof(char)*j ); *(p2+j-1) = a; j++; } p2 = (char *)realloc(p2, sizeof(char)*j); //为字符串结束标志多开辟一个字节的空间 *(p2+j-1) = '/0'; //添加字符串结束标志 p1 = (char *)realloc(p1, sizeof(char)*(i+j-1)); for(k=0;k<j;k++,i++) //将第二个字符串连接到第一个字符串的后面 { *(p1+i-1) = *(p2+k); } printf("%s/n", p1); return 0; }
#include<stdio.h> int main() { char a[100],b[100],c[100]; scanf("%s%s",a,b); int i,j,k; for(i=0;i<100;i++)//存储a { if(a[i]!='\0') c[i]=a[i]; else break; } for(k=0,j=i;j<200;j++,k++)//存储b { if(b[k]!='\0') c[j]=b[k]; else break; } c[j]='\0';//别忘记结尾的\0 printf("%s",c); }
//关键在于不用Strlen求字符串长度,需要自己编写代码 //无冗余需要用到动态数组new #include<iostream> #include<stdlib.h> using namespace std; //用递归求字符串长度,字符串都以'\0'结尾 int strlen1(char * ch){ if(*ch!='\0')return strlen1(ch+1)+1; else return 0; } int main(){ char *ch1=new char [100]; //不超过100 char *ch2=new char [100]; cin>>ch1>>ch2; int len1=strlen1(ch1),len2=strlen1(ch2); int len=len1+len2+1; char *ch=new char[len]; int i=0,j=0;; for(;i<len1;i++) ch[i]=ch1[i]; for(j=0;j<len2;j++) ch[i+j]=ch2[j]; ch[i+j]='\0'; cout<<ch; delete[]ch; delete []ch1; delete []ch2; }
#include <cstdlib> #include <iostream> using namespace std; int main(){ char ch; int i=0; //用i来记录字符串长度 char* str=(char*)malloc(sizeof(char)); while((ch=getchar())!=' '&&ch!='\n'){ //遇见空格或者换行结束循环 str=(char*)realloc(str,sizeof(char)*(++i)); //再分配内存,以便存入新的字符 *(str+i-1)=ch; } while((ch=getchar())!=' '&&ch!='\n'){ //第二个字符串存入 str=(char*)realloc(str,sizeof(char)*(++i)); *(str+i-1)=ch; } str=(char*)realloc(str,sizeof(char)*(++i)); //存入最后一个结束符 *(str+i-1)='\0'; cout<<str<<endl; free(str); //释放内存空间 return 0; }
#include <iostream> #include <cstdio> using namespace std; const int maxn=101; int main() { char str1[maxn],str2[maxn]; scanf("%s %s",str1,str2); printf("%s%s",str1,str2); return 0; }