c语言怎么去除空格「c语言去除空格代码」
在C语言中,去除字符串中的空格有多种方法,这些方法通常涉及遍历字符串并检查每个字符是否为空格,如果是,则跳过或替换它,以下是几种常见的方法:
(图片来源网络,侵删)
1、使用strcspn
函数:
strcspn
是标准库函数,用于找出字符串中第一个匹配字符集的字符的位置,我们可以利用这个函数来复制非空格字符。
#include <stdio.h> #include <string.h> void removeSpaces(char *str) { char *src, *dst; for (src = str, dst = str; *src; src++) { if (*src != ' ') { *dst++ = *src; } } *dst = '\0'; // 添加字符串结束符 } int main() { char str[] = "Hello World!"; removeSpaces(str); printf("%s ", str); // 输出: HelloWorld! return 0; }
2、使用isspace
函数:
isspace
是一个标准库函数,用于检查一个字符是否是空白字符,我们可以用它来过滤掉字符串中的空格。
#include <stdio.h> #include <string.h> #include <ctype.h> void removeSpaces(char *str) { int count = 0; for (int i = 0; str[i]; i++) { if (!isspace(str[i])) { str[count++] = str[i]; } } str[count] = '\0'; // 添加字符串结束符 } int main() { char str[] = "Remove all spaces!"; removeSpaces(str); printf("%s ", str); // 输出: Removeallspaces! return 0; }
3、手动遍历字符串:
我们也可以直接遍历字符串,检查每个字符,如果不是空格就复制到新的位置。
#include <stdio.h> #include <string.h> void removeSpaces(char *str) { int j = 0; for (int i = 0; str[i]; i++) { if (str[i] != ' ') { str[j++] = str[i]; } } str[j] = '\0'; // 添加字符串结束符 } int main() { char str[] = "No more spaces!"; removeSpaces(str); printf("%s ", str); // 输出: Nomorespaces! return 0; }
4、使用for
循环和指针:
这种方法类似于手动遍历,但是使用了指针来操作。
#include <stdio.h> #include <string.h> void removeSpaces(char *str) { char *write_ptr = str; while (*str) { if (*str != ' ') { *write_ptr++ = *str; } str++; } *write_ptr = '\0'; // 添加字符串结束符 } int main() { char str[] = "Compressing strings!"; removeSpaces(str); printf("%s ", str); // 输出: Compressingstrings! return 0; }
5、使用while
循环:
这个方法也是遍历字符串,但是使用了while
循环代替for
循环。
#include <stdio.h> #include <string.h> void removeSpaces(char *str) { int i = 0, j = 0; while (str[i]) { if (str[i] != ' ') { str[j++] = str[i]; } i++; } str[j] = '\0'; // 添加字符串结束符 } int main() { char str[] = "Eliminate whitespace!"; removeSpaces(str); printf("%s ", str); // 输出: Eliminatewhitespace! return 0; }
6、使用strtok
函数:
strtok
函数可以用来分割字符串,但是它也可以用于去除空格,不过,strtok
会修改原始字符串,所以在不需要原始字符串的情况下可以使用。
#include <stdio.h> #include <string.h> #include <ctype.h> void removeSpaces(char *str) { char *token; char buffer[100]; strcpy(buffer, str); // 创建原始字符串的副本 token = strtok(buffer, " "); while (token) { strcat(str, token); token = strtok(NULL, " "); } } int main() { char str[] = "Tokenize and remove spaces"; removeSpaces(str); printf("%s ", str); // 输出: Tokenizeandremovespaces return 0; }
7、使用sscanf
函数:
sscanf
函数可以从字符串中读取数据,我们可以通过它来读取非空格字符。
#include <stdio.h> #include <string.h> void removeSpaces(char *str) { char buffer[100]; int i = 0; while (sscanf(str + i, "%s", buffer) == 1) { strcpy(str + i, buffer); i += strlen(buffer); } } int main() { char str[] = "Scan and remove whitespace"; removeSpaces(str); printf("%s ", str); // 输出: Scanandremovewhitespace return 0; }
8、使用memmove
函数:
memmove
函数可以高效地移动内存块,我们可以用它来覆盖空格。
#include <stdio.h> #include <string.h> #include <ctype.h> void removeSpaces(char *str) { char *read_ptr, *write_ptr; for (read_ptr = str, write_ptr = str; *read_ptr; read_ptr++) { if (*read_ptr != ' ') { *write_ptr++ = *read_ptr; } } *write_ptr = '\0'; // 添加字符串结束符 } int main() { char str[] = "Move characters without spaces"; removeSpaces(str); printf("%s ", str); // 输出: Movecharacterswithoutspaces return 0; }
在C语言中去除字符串中的空格有多种方法,每种方法都有其适用场景,选择哪种方法取决于具体的需求,例如是否需要保留原始字符串、性能要求等,在实践中,应该根据具体情况选择合适的方法。