回 帖 发 新 帖 刷新版面

主题:串操作?



#define maxnum 10
typedef struct
{
    char str[maxnum];
    int len;
}strtype;
void strcat(strtype s,strtype l,strtype t)
{
    int i;
    if(s.len+l.len<maxnum)//串的长度越界
    {
        for(i=0;i<s.len;i++)  //将串s的值给新串t
            t.str[i]=s.str[i];
        for(i=0;i<l.len;i++)  //将串l的值给新串t
            t.str[i]=l.str[i];
        for(i=0;i<s.len+l.len;i++) //打印新串t
            printf("%c",t.str[i]);
    }
    else
        printf("overflow");
}
void mian()
{
    strtype s,l,t;
    int i=0,j=0;
    char ch;
    while((ch=getchar())!='\n')//输入字符串s
        s.str[++i]=ch;
    s.len=i;
    while((ch=getchar())!='\n')//输入字符串l
        l.str[++j]=ch;
    l.len=j;
    strcat(s,l,t);//合并//为什么这里提示出错:local variable 't' used without having been initialized?
}

回复列表 (共2个回复)

沙发

//abc.cpp在VC++上用C++运行
#include "stdio.h"
#define maxnum 10
typedef struct
{
    char str[maxnum];
    int len;
}strtype;
void strcat(strtype s,strtype l)
{
    strtype t;
    int i,j;
    if(s.len+l.len<maxnum)//串的长度越界
    {
        for(i=0;i<s.len;i++)  //将串s的值给新串t
            t.str[i]=s.str[i];
        for(j=0;j<l.len;j++,i++)  //将串l的值给新串t
            t.str[i]=l.str[j];
        for(i=0;i<s.len+l.len;i++) //打印新串t
            printf("%c",t.str[i]);
        printf("\n");
    }
    else
        printf("overflow");
}
void main()
{
    strtype s,l;
    int i=0,j=0;
    char ch;
    while((ch=getchar())!='\n')//输入字符串s
        s.str[i++]=ch;
    s.len=i;
    while((ch=getchar())!='\n')//输入字符串l
        l.str[j++]=ch;
    l.len=j;
    strcat(s,l);
}

板凳

你的t是从何而来的
主函数里面没有输入t的语句

我来回复

您尚未登录,请登录后再回复。点此登录或注册