💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# C 程序:删除字符串中除字母之外的所有字符 > 原文: [https://www.programiz.com/c-programming/examples/remove-characters-string](https://www.programiz.com/c-programming/examples/remove-characters-string) #### 在此示例中,您将学习从用户输入的字符串中删除除字母之外的所有字符。 要理解此示例,您应该了解以下 [C 编程](/c-programming "C tutorial")主题: * [C 数组](/c-programming/c-arrays) * [C 编程字符串](/c-programming/c-strings) * [C `for`循环](/c-programming/c-for-loop) * [C `while`和`do...while`循环](/c-programming/c-do-while-loops) * * * ## 删除字符串中除字母之外的字符 ```c #include <stdio.h> int main() { char line[150]; printf("Enter a string: "); fgets(line, sizeof(line), stdin); // take input for (int i = 0, j; line[i] != '\0'; ++i) { // enter the loop if the character is not an alphabet // and not the null character while (!(line[i] >= 'a' && line[i] <= 'z') && !(line[i] >= 'A' && line[i] <= 'Z') && !(line[i] == '\0')) { for (j = i; line[j] != '\0'; ++j) { // if jth element of line is not an alphabet, // assign the value of (j+1)th element to the jth element line[j] = line[j + 1]; } line[j] = '\0'; } } printf("Output String: "); puts(line); return 0; } ``` **输出** ```c Enter a string: p2'r-o@gram84iz./ Output String: programiz ``` * * * 该程序从用户处获取一个字符串输入,并将其存储在`line`变量中。 然后,使用`for`循环迭代字符串的字符。 如果字符串中的字符不是字母,则将其从字符串中删除,其余字符的位置向左移动 1 个位置。