数据结构串的使用

在复制的串的时候为何只复制第一个s串的第一个字母,求解

#include<stdio.h>
#include<stdlib.h>
#define maxSize 256   //串字符数组最大长度 
typedef struct{
    char ch[maxSize+1];    //顺序串的存储数组 
    int n;                //顺序串的实际长度 
}SeqString; 
//建立字符串
void createSeqString(SeqString &s, char *ch1){
    int i=0;
    while (i < maxSize &&s.ch[i]!='\0'){
        s.ch[i]=ch1[i];
        i++;
    }
    s.n=i;
} 
//输出字符串
void printSeqString(SeqString &s) {
    for(int i=0;i<s.n;i++){
        if(s.ch[i]=='\0')
            break;
        else printf("%c",s.ch[i]);
    }
}
//复制字符串
int copySeqString(SeqString &s,SeqString &t){
    for(int i=0;i<s.n;i++){
        t.ch[i]=s.ch[i];
    }
} 

main(){
    SeqString s;
    SeqString t;
    char ch1[s.n];
    printf("请输入字符串:");
    gets(ch1);
    createSeqString(s,ch1);
    printf("字符串的长度:%d\n",s.n);
    printSeqString(s);
    printf("复制后t的字符串为:");
    copySeqString(s,t);
    puts(t.ch);
}

 

posted @ 2019-05-15 00:16  吕志琪  阅读(622)  评论(1编辑  收藏  举报