【问题标题】:getting ╠ when printing in C [duplicate]在 C 中打印时获得╠ [重复]
【发布时间】:2018-08-18 20:07:14
【问题描述】:

我已经搜索并找到了解决方案,但我无法弄清楚为什么我的代码仍然无法正常工作,如果我理解正确,我的 check2 字符串没有'\0'。 我正在使用视觉工作室 我知道有一种更好的写作方法和简短的方法,但只是尝试练习一些事情并尝试更加努力地做到这一点。 我是 C 新手,所以不要评判我:P 这是我的代码:

#include <stdio.h>
#include <string.h>

/*
A palindrome is a string that is same in both forward and backward reading.
Example:
   "madam"
   "racecar"
   "a man a plan a canal panama"
   "radar"
You will write a program that will test if a given string is a palingdrome or not.
Your program will ask the user to input a string and if the string is a palindrome program
will just print "Yes, it is a Palindrome", otherwise will print "No, not a Palindrome".

Please note that:
1. Your you need to check in case-insensitive way, that means: Madam or madam both should be
detected as Palindrome.

2. There can be (any number of ) spaces in between the words.
    "A man a plan a canal panama"
        OR
    "A     man    a   pla n a cana l Panama"
    both the strings must be detected as Palindrome.
3.There can be punctuations in between the words, for this assignments,
we consider only 4 punctuations,   . ?  ! and ,

Your program will just need to ignore them (treat them as space).
    "Cigar? Toss it in a can. It is so tragic."
    Should be detected as palindrome.

 *** For this assignment I will not write any instructions or guidance, you are free
        to implement it with your own way, you can use the string.h functions

    Good luck.

*/


int main(){
    char string [100];
    printf("Enter a string: ");
    scanf("%[^\n]", string);

    int isPalindrome = 1;  // assign 0 to this if the string is a NOT palindrome

    // write code to test if string is a palindrome

    char check1[100], check2[100];
    int i, j = 0;

    for (i = 0; string[i] != '\0'; i++, j++)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check1[i] = string[i];
        }
    }

    check1[i] = '\0';

    for (i = strlen(string); i < 0; i--)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check2[i] = string[i];
        }
    }

    i = strlen(string);
    check2[i] = '\0';

    for (i = 0; check1[i] != '\0'; i++)
    {
        if ((check1[i] >= 65) && (check1[i] <= 90))
            check1[i] = check1[i] + 32;
    }

    for (i = 0; check2[i] != '\0'; i++)
    {
        if ((check2[i] >= 65) && (check2[i] <= 90))
            check2[i] = check2[i] + 32;
    }

    printf("%s\n", check1);
    printf("%s\n", check2);

    isPalindrome = strcmp(check1, check2);

    // at the end you need to test
    if (isPalindrome){
        printf("Yes, it is Palindrome!\n");
    }
    else{
        printf("No, not a Palindrome\n");
    }


    return 0;
}

这是我的输出:

Enter a string: MadaM
madam
╠╠╠╠╠
Yes, it is Palindrome!
Press any key to continue . . .

好的,现在我的代码如下所示:

#include <stdio.h>
#include <string.h>

/*
A palindrome is a string that is same in both forward and backward reading.
Example:
   "madam"
   "racecar"
   "a man a plan a canal panama"
   "radar"
You will write a program that will test if a given string is a palingdrome or not.
Your program will ask the user to input a string and if the string is a palindrome program
will just print "Yes, it is a Palindrome", otherwise will print "No, not a Palindrome".

Please note that:
1. Your you need to check in case-insensitive way, that means: Madam or madam both should be
detected as Palindrome.

2. There can be (any number of ) spaces in between the words.
    "A man a plan a canal panama"
        OR
    "A     man    a   pla n a cana l Panama"
    both the strings must be detected as Palindrome.
3.There can be punctuations in between the words, for this assignments,
we consider only 4 punctuations,   . ?  ! and ,

Your program will just need to ignore them (treat them as space).
    "Cigar? Toss it in a can. It is so tragic."
    Should be detected as palindrome.

 *** For this assignment I will not write any instructions or guidance, you are free
        to implement it with your own way, you can use the string.h functions

    Good luck.

*/


int main(){
    char string [100];
    printf("Enter a string: ");
    scanf("%[^\n]", string);

    int isPalindrome = 1;  // assign 0 to this if the string is a NOT palindrome

    // write code to test if string is a palindrome

    char check1[100], check2[100];
    int i, j, dess = 0, len;

    for (i = 0; string[i] != '\0'; i++)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check1[dess++] = string[i];
        }
    }

    check1[dess] = '\0';



    /*
    len = strlen(string);


    for (j = 0, i = 0; j < len; j++)
    {
        if ((string[j] >= 'A' && string[j] <= 'Z') || (string[j] >= 'a' && string[j] <= 'z'))
        {
            check2[i++] = string[len - j];
        }
    }
    */

    for (dess = 0, i = strlen(string) - 1; i >= 0; i--)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check2[dess++] = string[i];
        }
    }

    check2[dess] = '\0';

    for (i = 0; check1[i] != '\0'; i++)
    {
        if ((check1[i] >= 65) && (check1[i] <= 90))
            check1[i] = check1[i] + 32;
    }   

    for (i = 0; check2[i] != '\0'; i++)
    {
        if ((check2[i] >= 65) && (check2[i] <= 90))
            check2[i] = check2[i] + 32;
    }

    printf("%s\n", check1);
    printf("%s\n", check2);
    for (i = 0; check1[i] != '\0'; i++)
    {
        printf("%c\n", check1[i]);
    }
    for (i = 0; check2[i] != '\0'; i++)
    {
        printf("%c\n", check2[i]);
    }

    isPalindrome = strcmp(check1, check2);

    // at the end you need to test
    if (isPalindrome){
        printf("Yes, it is Palindrome!\n");
    }
    else{
        printf("No, not a Palindrome\n");
    }


    return 0;
}

但我的输出是:

Enter a string: MadaM
madam
madam
m
a
d
a
m
m
a
d
a
m
No, not a Palindrome
Press any key to continue . . .

为什么说它不是回文?

【问题讨论】:

  • for (i = strlen(string); i &lt; 0; i--) 从不执行:i&lt;0 条件错误。应该是i&gt;=0。并且应该从strlen(string)-1 开始
  • ╠╠╠╠╠ 是一个神奇的数字,表示代码中存在某种广泛传播的错误。有一个重复的问题,我现在找不到。
  • 啊,我想你在这里找这个问题@Yummosch stackoverflow.com/questions/34931221/…
  • 症状相同,原因不同。
  • ╠ 在代码页 437 中为 0xCC,MSVC fills 0xCC to uninitialized memory to help debugging。这意味着您访问了未初始化的内存。你可以在这里找到关于 ╠ 和 0xCC 的大量问题

标签: c palindrome


【解决方案1】:

这个循环根本没有执行:

for (i = strlen(string); i < 0; i--)
{
    if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
    {
        check2[i] = string[i];
    }
}

因为i &lt; 0 条件是“继续”条件,而不是“停止”条件。此外,i 在字符串数据之外开始。

所以check2 永远不会被初始化,这就解释了垃圾。

固定循环是:

for (i = strlen(string) - 1; i >= 0; i--)

那些反向循环结构很容易出错。我建议一个正向循环并计算循环中的反向索引,更清晰,如下所示:

int len = strlen(string);
for (j = 0; j < len; j++)
{
    i = len-j-1;   // starts at the last char, ends at the first char

在这种情况下,不需要反向循环,因为前向循环会达到完全相同的结果(这里没有 break 或其他索引)

【讨论】:

  • 前向循环要好得多,但我仍然想继续使用我的反向循环,现在我已经改变了所有工作,但是当我把 MadaM 说它不是回文时,我的代码 (dess = 0, i = strlen(string) - 1; i >= 0; i--) { if ((string[i] >= 'A' && string[i] = 'a' && string[i]
【解决方案2】:

正如其他人指出的那样,您的循环索引和限制存在一些问题。

除此之外,您在过滤字母时遇到了一些问题:

    for (i = 0; string[i] != '\0'; i++, j++)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check1[i] = string[i];
        }
    }

首先,您可以使用函数isalpha() 来检查字母。 其次,您认为中间的空间会发生什么? 您只需将任何字母复制到您的数组check,就在它在输入字符串中的相同位置。 这没有意义。

您需要第二个目标索引变量::

    int dest = 0;
    for (i = 0; string[i] != '\0'; i++)
    {
        if ((string[i] >= 'A' && string[i] <= 'Z') || (string[i] >= 'a' && string[i] <= 'z'))
        {
            check1[dest++] = string[i];
        }
    }

其他循环类似

【讨论】:

  • 这已经比我修复 OP 第一个问题的答案要好......但不能解决其余问题。好吧,也许我们应该用一个重复的链接完全结束这个问题
  • 好吧,骗子只是告诉我们有一些未初始化的变量,而没有为 OP 解决任何问题。这将要求立即跟进问题“我在哪里错过初始化?”
  • 这里有答案 + 重复的 OP 已经整理好了,这个问题并没有真正为网站增加价值。
  • 是的,如果保留答案就足够了
  • 哦,对了,你没有考虑到这一点,所以我像这里的所有建议一样更改了我的代码,当我打印字符串时它的打印相同,但是当我输入 MadaM 时,它说它不是回文。
猜你喜欢
  • 2016-03-12
  • 1970-01-01
  • 1970-01-01
  • 2020-01-24
  • 1970-01-01
  • 1970-01-01
  • 2017-04-21
  • 2016-05-10
  • 1970-01-01
相关资源
最近更新 更多