0
我做了一個代碼,用於在指定的位置插入用戶輸入的字符串。例如,如果字符串是:愛麗絲仙境,單詞在和pos是6 ,op /應該是:alicein仙境 但是,代碼不起作用。你能告訴我缺陷嗎? 下面的代碼:幫助在指定的位置插入字符串中的字符
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
char str[100],word[50];
int i,len,p,f,w;
clrscr();
cout<<"Enter string"<<endl;
gets(str);
cout<<"Enter word and position"<<endl;
gets(word);
cin>>p;
for(len=0;str[len!='\0';len++);
for(w=0;word[w]!='\0';w++);
for(i=len-1;i>=p-1;i--)
str[i+w]=str[i];
for(i=p-1;f=0;f<w;f++,i++)
str[i]=word[f];
str[len+w-1]='\0';
cout<<"Modified string..."<<endl;
puts(str);
getch();
}
您應該開始使用C++風格。這意味着包括沒有擴展名的標題。例如。 '#include'和'#include '。你應該爲你的IO使用'cin','cout','getline()'等,而不是像'gets()'這樣的'stdio.h'。您應該在使用它們之前聲明變量,而不是在函數的開頭。此外,一些空格(空白行)和評論會很好,讓讀者的大腦休息一下。 –
我會先解決你的編譯錯誤。 – aib