消除字符串中停在空格处的特殊字符
Eliminating special characters of a string stops at spaces
我是 C 的初学者,但我正在尝试制作一个脚本,该脚本读取输入并忽略特殊字符和 spaces,无论字母是否构成回文,都输出与输入相反的内容。
我已经尝试调整 fix 函数中循环的长度,因为我认为这就是问题所在,但据我所知,strlen() 正在按预期工作,循环只是在遇到时停止space.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 1000 /* The maximum number of characters in a line of
input
*/
void fix(char str[], char str2[]);
int main()
{
char text[MAX], text2[MAX], text3[MAX], text4[MAX], temp;
int i, j;
int length;
puts("Type some text (then ENTER):");
/* Save typed characters in text[]: */
fgets(text, MAX, stdin);
length = strlen(text) - 1;
strcpy(text2, text);
i = 0;
j = length-1;
while(i < j){
temp = text[i];
text[i] = text[j];
text[j] = temp;
i++;
j--;
}
/* Analyse contents of text[]: */
printf("Your input in reverse is:\n");
printf("%s", text);
fix(text, text3);
printf("%s\n", text3);
fix(text2, text4);
printf("%s\n", text4);
if(strcmp(text4, text3) == 0)
printf("Found a palindrome!\n");
return 0;
}
void fix(char str[], char str2[]){
int i;
for(i = 0; i < strlen(str)-1; i+=1){
if (isalpha(str[i])){
str2[i] = tolower(str[i]);
}
}
}
对于"Nurses, run"的输入,反转后的字符串正确输出,但是没有"Found a palindrome!"
的输出
打印文本 3 和 4 分别打印 "nur" 和 "nurses,"。
似乎循环在遇到 space 时停止,但我无法根据完整输入的长度找出原因。
您的 fix
函数中存在问题导致未定义的行为,因为您只初始化了结果字符串中字母字符的位置。
因为您的要求是忽略 非字母字符,所以您需要一个额外的计数器。如果必须从字符串中删除字符,显然 i
会指向错误的字符。所以你需要统计你实际复制了多少个字符到str2
.
void fix(char str[], char str2[])
{
int i, copied = 0;
for(i= 0; str[i] != '[=10=]'; i++) // See note 1
{
if (isalpha(str[i])) {
str2[copied++] = tolower(str[i]); // See note 2
}
}
str2[copied] = '[=10=]'; // See note 3
}
备注:
- 不需要每次测试都调用
strlen
。事实上,您根本不需要调用它——这只是测试字符串的空终止符。
- 注意
copied
在这里递增。这样可以保证您在排除字符时不会有间隙。
- 您必须记住以 null 终止您的新字符串。
我是 C 的初学者,但我正在尝试制作一个脚本,该脚本读取输入并忽略特殊字符和 spaces,无论字母是否构成回文,都输出与输入相反的内容。
我已经尝试调整 fix 函数中循环的长度,因为我认为这就是问题所在,但据我所知,strlen() 正在按预期工作,循环只是在遇到时停止space.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAX 1000 /* The maximum number of characters in a line of
input
*/
void fix(char str[], char str2[]);
int main()
{
char text[MAX], text2[MAX], text3[MAX], text4[MAX], temp;
int i, j;
int length;
puts("Type some text (then ENTER):");
/* Save typed characters in text[]: */
fgets(text, MAX, stdin);
length = strlen(text) - 1;
strcpy(text2, text);
i = 0;
j = length-1;
while(i < j){
temp = text[i];
text[i] = text[j];
text[j] = temp;
i++;
j--;
}
/* Analyse contents of text[]: */
printf("Your input in reverse is:\n");
printf("%s", text);
fix(text, text3);
printf("%s\n", text3);
fix(text2, text4);
printf("%s\n", text4);
if(strcmp(text4, text3) == 0)
printf("Found a palindrome!\n");
return 0;
}
void fix(char str[], char str2[]){
int i;
for(i = 0; i < strlen(str)-1; i+=1){
if (isalpha(str[i])){
str2[i] = tolower(str[i]);
}
}
}
对于"Nurses, run"的输入,反转后的字符串正确输出,但是没有"Found a palindrome!"
的输出打印文本 3 和 4 分别打印 "nur" 和 "nurses,"。
似乎循环在遇到 space 时停止,但我无法根据完整输入的长度找出原因。
您的 fix
函数中存在问题导致未定义的行为,因为您只初始化了结果字符串中字母字符的位置。
因为您的要求是忽略 非字母字符,所以您需要一个额外的计数器。如果必须从字符串中删除字符,显然 i
会指向错误的字符。所以你需要统计你实际复制了多少个字符到str2
.
void fix(char str[], char str2[])
{
int i, copied = 0;
for(i= 0; str[i] != '[=10=]'; i++) // See note 1
{
if (isalpha(str[i])) {
str2[copied++] = tolower(str[i]); // See note 2
}
}
str2[copied] = '[=10=]'; // See note 3
}
备注:
- 不需要每次测试都调用
strlen
。事实上,您根本不需要调用它——这只是测试字符串的空终止符。 - 注意
copied
在这里递增。这样可以保证您在排除字符时不会有间隙。 - 您必须记住以 null 终止您的新字符串。