字符串出现乱码,c++小萌新求助

 #include <iostream>
#include <string.h>
#include <cstdio>
using namespace std;

void rep(char *str,char *fstr,char *rstr)
{
    int i,j,k,n,x,y=0,cnt=0;
    char sss[50];
    n=strlen(fstr);
    x=strlen(rstr);
    for(i=0;str[i]!='\0';i++) {
        if(str[i]==fstr[0]) {
                k=1;
                j=i+1;
            while(j<i+n) {
                 if(str[j]==fstr[k]) {
                    j++;
                    k++;
                 }
                 else break;
            }
            if(j==i+n) {
               strcat(sss,rstr);
               cnt++;
               i=j-1;
               y=j+(x-n)*cnt-1;
            }
        }
        else sss[y]=str[i];
        y++;
    }

    sss[y]='\0';
    strcpy(str,sss);
}
int main()
{
    char str[50]="iffordowhileelsewhilebreak";
    char fstr[10]="while";
    char rstr[10]="struct";
    /*gets(str);
    gets(fstr);
    gets(rstr);*/
    rep(str,fstr,rstr);
    cout<<str<<endl;
    return 0;
}

https://img3.mukewang.com/5b5acf320001e45f12780458.jpg

https://img3.mukewang.com/5b5acf3e00017fe610120537.jpg

运行后出现乱码。

ABOUTYOU
浏览 899回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP