主题:<求助>关于串的连接
高手帮看下,我连接S1和S2两个字符串,字符数组有效位数最大为255,但是当我输入的S2的位数大于S1的时候,程序只截取S2的位数,而不是S1+S2的位数(和不大于255),以下为代码
#include <stdio.h>
#include <string.h>
#define OVERFLOW -2
#define ERROR 0
#define OK 1
#define TRUE 1
#define FALSE 0
#define MAXSTRLEN 225
typedef int Status;
typedef unsigned char SString[MAXSTRLEN+1];
int SStrlen (SString S)
{
int len;
len=1;
if (S){//存在S
while (S[len]!='\0')
len++;
return len;
}
return ERROR;
}
void Concat (SString &T,SString S1,SString S2){
int s1_len,s2_len,i,j;
s1_len=SStrlen (S1);
s2_len=SStrlen (S2);
i=0,j=0;
printf("s1_len=%d, s2_len=%d\n",s1_len,s2_len);
if ( s1_len + s2_len <= MAXSTRLEN) {
while (S1[i]!='\0') {
T[j]=S1[i];
i++; j++;
}//S1赋值
i=0;
while (S2[j]!='\0') {
T[j]=S2[i];
i++; j++;
}//S2赋值
T[j]='\0';
}
else if ( s1_len < MAXSTRLEN) {
while (S1[i]!='\0') {
T[j]=S1[i];
i++; j++;
}//S1赋值
for (i=0;i<MAXSTRLEN-s1_len ;i++,j++)
T[j]=S2[i];
T[j]='\0';
}
else if (s1_len==MAXSTRLEN) {
while (S1[i]!='\0') {
T[j]=S1[i];
i++; j++;
}//S1赋值
}
}
void main()
{
SString S1,S2,T;
int i=0,j=0,T_len;
printf("输入S1:");
while ((S1[i]=getchar())!='\n')
i++;
S1[i]='\0';
printf("输入S2:");
while ((S2[j]=getchar())!='\n')
j++;
S2[j]='\0';
Concat(T,S1,S2);
T_len=SStrlen (T);
printf("%s\n",T);
printf("T_length=%d\n",T_len);
}
#include <stdio.h>
#include <string.h>
#define OVERFLOW -2
#define ERROR 0
#define OK 1
#define TRUE 1
#define FALSE 0
#define MAXSTRLEN 225
typedef int Status;
typedef unsigned char SString[MAXSTRLEN+1];
int SStrlen (SString S)
{
int len;
len=1;
if (S){//存在S
while (S[len]!='\0')
len++;
return len;
}
return ERROR;
}
void Concat (SString &T,SString S1,SString S2){
int s1_len,s2_len,i,j;
s1_len=SStrlen (S1);
s2_len=SStrlen (S2);
i=0,j=0;
printf("s1_len=%d, s2_len=%d\n",s1_len,s2_len);
if ( s1_len + s2_len <= MAXSTRLEN) {
while (S1[i]!='\0') {
T[j]=S1[i];
i++; j++;
}//S1赋值
i=0;
while (S2[j]!='\0') {
T[j]=S2[i];
i++; j++;
}//S2赋值
T[j]='\0';
}
else if ( s1_len < MAXSTRLEN) {
while (S1[i]!='\0') {
T[j]=S1[i];
i++; j++;
}//S1赋值
for (i=0;i<MAXSTRLEN-s1_len ;i++,j++)
T[j]=S2[i];
T[j]='\0';
}
else if (s1_len==MAXSTRLEN) {
while (S1[i]!='\0') {
T[j]=S1[i];
i++; j++;
}//S1赋值
}
}
void main()
{
SString S1,S2,T;
int i=0,j=0,T_len;
printf("输入S1:");
while ((S1[i]=getchar())!='\n')
i++;
S1[i]='\0';
printf("输入S2:");
while ((S2[j]=getchar())!='\n')
j++;
S2[j]='\0';
Concat(T,S1,S2);
T_len=SStrlen (T);
printf("%s\n",T);
printf("T_length=%d\n",T_len);
}