首页 文章

尝试从字符串中删除空格?

提问于
浏览
1

这个问题是关于我正在为Ada中的编程类介绍的程序 . 我可以使用的唯一包是基本的text,integer和float包 . 我正在编写一个程序来删除字符串中的空格,并且我在同一行上不断出现约束错误 . 这是一行:

if inputString(count) /= Character'Val(32) then

我将count设置为从1开始,然后通过循环递增,直到字符串结束,然后检查字符串的每个字符以查看它是否与空格的字符值不同(32) . 然后我将包括这一行:

noSpace(count..count) := inputString(count..count)

其中noSpace是没有空格的字符串 . 我的思路可能完全没有了,我可能需要从一个完全不同的角度来解决这个问题 .

Update :我现在设法通过错误消息并成功删除了我的字符串中的空格 . 但是现在当我打印新字符串时,后面会出现各种各样的字符和阴影空格 . 我认为问题是由于新字符串比前一个字符串短并且最后一个字符不同而引起的 .

4 回答

  • 3

    可能是这个bug被隐藏在其他地方 .

    试试这个:

    with Ada.Text_IO;
    
    procedure String_Remove_Space is
    
        function Count_Space( S : String ) return Natural is
            Sum_Of_Space : Natural := 0;
        begin
            for I in S'range loop
                if S(I) = ' ' then
                    Sum_Of_Space := Sum_Of_Space + 1;
                end if;
            end loop;
            return Sum_Of_Space;
        end Count_Space;
    
        Input_String : String := "Apple is on the tree";
        Input_No : String := "AppleIsOnTheTree";
    
        No_Space : String(1..Input_String'Length - Count_Space(Input_String));
    
        Index : Positive := 1;
    begin
    
        for I in Input_String'range loop
            if Input_String(I) /= ' ' then
                No_Space(Index) := Input_String(I);
                Index := Index + 1;
            end if;
        end loop;
    
        Ada.Text_IO.Put_Line(Input_String);
        Ada.Text_IO.Put_Line(No_Space);
    
    end String_Remove_Space;
    
  • 1

    如果输入字符串中有空格,则输出字符串的有效部分必须更短 . 我想你写了类似的东西

    noSpace : String := inputString;
    

    只要在 inputString 中没有空格就需要 noSpace 来开始关闭?然后,当您挤出空格时,您似乎正在写垃圾(未定义的字符)到 noSpace 的末尾 . 只要您只处理有效部分,这无所谓 .

    我试过这个:

    with Ada.Text_IO; use Ada.Text_IO;
    procedure Lareaper is
       function Strip_Space (S : String) return String is
          Result : String := S;
          Current : Positive := Result'First;
          Last : Natural := Result'Last;
       begin
          loop
             exit when Current > Last; --  processed the whole string
             if Result (Current) = ' ' then
                --  slide the rest of the string back one
                Result (Current .. Last - 1) := Result (Current + 1 .. Last);
                --  which reduces the length by 1 too
                Last := Last - 1;
             else
                --  non-space character, skip
                Current := Current + 1;
             end if;
          end loop;
          --  return only the part of the result that doesn't contain spaces
          return Result (Result'First .. Last);
       end Strip_Space;
    begin
       Put_Line ('|' & Strip_Space ("") & '|');
       Put_Line ('|' & Strip_Space (" ") & '|');
       Put_Line ('|' & Strip_Space ("a") & '|');
       Put_Line ('|' & Strip_Space ("ab") & '|');
       Put_Line ('|' & Strip_Space (" a") & '|');
       Put_Line ('|' & Strip_Space ("a ") & '|');
       Put_Line ('|' & Strip_Space ("a b") & '|');
       Put_Line ('|' & Strip_Space (" a b ") & '|');
    end Lareaper;
    

    哪个输出

    $ ./lareaper 
    ||
    ||
    |a|
    |ab|
    |a|
    |a|
    |ab|
    |ab|
    
  • 1

    Count应该从InputString'First开始,而不是从1开始 . 特别是,当InputString被创建为另一个字符串的slice / substring时,很可能它的第一个字符不在索引1处 .

  • 2

    只是为了向您展示另一种可能的解决方案,尽管我不会在现实生活中使用它:

    function Strip_Space (S : String) return String is
    begin
        if S'Length = 0 then
            return "";
        elsif S (S'First) = ' ' then
            return Strip_Space (S (S'First + 1 .. S'Last));
        else
            return S (S'First) & Strip_Space (S (S'First + 1 .. S'Last));
        end if;
    end Strip_Space;
    

    然后就是我真正做到的方式 . 我没有仔细回答,以便意识到他正在滑动子串 . 我认为,最有效的答案类似于Mark 's, but you don' t实际上需要首先计算字符串中的空格 .

    function Strip_Space (Input_String : String) return String is
        No_Space : String(1..Input_String'Length);
    
        Index : Positive := 1;
    begin
    
        for I in Input_String'range loop
            if Input_String(I) /= ' ' then
                No_Space(Index) := Input_String(I);
                Index := Index + 1;
            end if;
        end loop;
    
        return No_Space(1 .. Index - 1);
    end Strip_Space;
    

相关问题