你能发现这段代码中的逻辑错误吗?计数

时间:2014-02-11 15:17:16

标签: c

我写的程序有问题。我不能为字符串输入正确的字数,但我得到最长的字符数正确。我不知道为什么,但这是我的代码。我正在做的是将一个字符串传递给一个函数,该函数将字符串中的所有字母大写。然后,如果它在A-Z范围内,则相同的函数逐个字符地确定,如果是,则添加一个名为charcount的最长连续字符A-Z计数。然后从那里它应该找到前一个字符是否不在A-Z范围内。如果不是,那么计算一个新词。最后的数组将计数传递给main函数。忽略所有额外的字符串。这里的重点是我的字数不合适,我找不到它。

#include <stdio.h>
void convert(char s[], int counts[]);

int main(void)
{
     int i = 0;
     int aray[2];
     char text0[] = "This is one of Several strings2use.";
     char text1[] = "This sample has less than 987654321 leTTers.";
     char text2[] = "Is thIs a string?  (definitely)";
     char text3[] = "Twitter loves its hashtags #twitterlove";
     char text4[] = "123 four five.";
     convert(text3,aray);
     printf("wordcount is %d and longest char is %d ", aray[0],aray[1]);




}

void convert(char s[], int counts[])
{
     int i = 0;

     while(s[i] != '\0')
     {
         if('a' <= s[i] && s[i] <= 'z')
          {   s[i] = s[i] - 32; }
         i = i + 1;
     }
     int h = 0;
     int wordcount = 0;
     int maxcount = 0;
     int charcount = 0;
     while(s[h] != '\0')
     {
         if('A'<= s[h]&& s[h] <= 'Z')
         {    
             charcount = charcount + 1;
             if ('A' >= s[h-1] && s[h-1] >= 'Z'); """if previous not in range"""
             {
                 wordcount = wordcount + 1;} """problem here"""
         }
         else
             charcount = 0;
         if(charcount>maxcount)
             {
             maxcount = charcount;
             }
        h = h+1;         

     }
         counts[0] = wordcount;
         counts[1] = maxcount;


}

2 个答案:

答案 0 :(得分:3)

在这一行:

if ('A' >= s[h-1] && s[h-1] >= 'Z');

... if语句的主体是“;”。它之后的块只是一个普通的块并且将始终执行。你需要删除分号。

答案 1 :(得分:3)

我刚刚编译了你的代码并启用了警告(gcc test.c -Wall),这就是我所得到的:

test.c: In function ‘main’:
test.c:12:11: warning: unused variable ‘text4’ [-Wunused-variable]
      char text4[] = "123 four five.";
           ^
test.c:10:11: warning: unused variable ‘text2’ [-Wunused-variable]
      char text2[] = "Is thIs a string?  (definitely)";
           ^
test.c:9:11: warning: unused variable ‘text1’ [-Wunused-variable]
      char text1[] = "This sample has less than 987654321 leTTers.";
           ^
test.c:8:11: warning: unused variable ‘text0’ [-Wunused-variable]
      char text0[] = "This is one of Several strings2use.";
           ^
test.c:6:10: warning: unused variable ‘i’ [-Wunused-variable]
      int i = 0;
          ^
test.c: In function ‘convert’:
test.c:40:51: warning: statement with no effect [-Wunused-value]
              if ('A' >= s[h-1] && s[h-1] >= 'Z'); """if previous not in range"""
                                                   ^
test.c:41:14: error: expected ‘;’ before ‘{’ token
              {
              ^
test.c: In function ‘main’:
test.c:19:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

这些警告通常有意义,而且通常有一点意义。因此,为了解决代码问题,您现在应该做的第一件事就是修复这些警告(和错误)。

然后,您应该学习使用调试工具(例如gdb)来逐步执行应用程序的执行路径,并在运行时检查变量的内容。除非你有一定的经验,通过有根据的猜测来相处,否则这就是让你自己识别逻辑问题的关键所在。

在编写实际代码之前,通过使用注释或伪代码概述算法来开始处理函数也很有帮助。当你得到一个似乎有意义的逻辑时,你可以根据需要填写实现。预先设计好的设计可以节省您数小时或数天的后期调试。