问:从字符串中删除所有空格的程序。

2025年1月8日 | 阅读需时 2 分钟

在此程序中,我们的任务是从字符串中删除所有空格。为此,我们需要遍历字符串,并检查字符串中的任何字符是否与空格字符匹配。如果是,则使用 replace() 等内置方法将其替换为空白。

在 C 语言中,我们没有内置的替换方法。因此,我们需要运行 for 循环来遍历字符串,并查看是否存在任何空格字符。如果存在,则从第 i 个字符开始到字符串末尾执行内循环 (j),并将每个元素替换为其相邻的元素。在此循环终止时,将字符串长度减 1。重复此过程,直到删除字符串中的所有空格。

算法

  1. 定义一个字符串。
  2. 使用 replace() 函数将所有空格字符替换为空白。
  3. 结果字符串之间将没有空格。

解决方案

Python

输出

String after removing all the white spaces : Removewhitespaces

C

输出

String after removing all the white spaces : Removewhitespaces

JAVA

输出

String after removing all the white spaces : Removewhitespaces

C#

输出

String after removing all the white spaces : Removewhitespaces

PHP

输出

String after removing all the white spaces : Removewhitespaces
 
下一个主题程序列表