2014-04-28 89 views
0

如果第一個字符串是Hello1 World,第二個必須是(Hello)1(World),但是我有(Hello)1()(World),我只需要在括號out必須使數字不在括號()數字是括號後(),我不需要。 如何解決這裏是我的代碼:我只需要括號中的字母

program task2; 
uses crt; 
var str,name:string; 
str2:^string; 
i:integer; 

begin 
clrscr; 


writeln('Enter string!'); 
writeln; 

readln(str); 
writeln; 

New(str2); 
str2^:=''; 
name:=''; 

for i:=1 to length(str) do 
if ((str[i]=' ') or (str[i]>='0') and (str[i]<='9')) then 
       begin 
       str2^:=str2^+'('+name+')'+str[i]; 
       name:=''; 
       end 
      else name:=name+str[i]; 
str2^:=str2^+'('+name+')'; 

writeln('First string: ',str); 
writeln('String length is ',length(str)); 
writeln; 
writeln('Second string: ',str2^); 
writeln('String length is ',length(str2^)); 

readln; 
end. 

謝謝。

回答

0

那麼問題是,即使當「名稱」爲空時,您也會追加「名稱」。 只需添加if length(name)>0這樣的:

program task2; 
uses crt; 
var str,name:string; 
str2:^string; 
i:integer; 

begin 
clrscr; 


writeln('Enter string!'); 
writeln; 

readln(str); 
writeln; 

New(str2); 
str2^:=''; 
name:=''; 

for i:=1 to length(str) do 
if ((str[i]=' ') or ((str[i]>='0') and (str[i]<='9'))) then 
    if (length(name)>0) then  
       begin 
       str2^:=str2^+'('+name+')'+str[i]; 
       name:=''; 
       end 
      else 
      else name:=name+str[i]; 
str2^:=str2^+'('+name+')'; 

writeln('First string: ',str); 
writeln('String length is ',length(str)); 
writeln; 
writeln('Second string: ',str2^); 
writeln('String length is ',length(str2^)); 

readln; 
end. 
相關問題