leetcode提交错误

may_Xia 2017-04-20 04:46:56
一个是520.Detect Capital
Given a word, you need to judge whether the usage of capitals in it is right or not.

We define the usage of capitals in a word to be right when one of the following cases holds:

All letters in this word are capitals, like "USA".
All letters in this word are not capitals, like "leetcode".
Only the first letter in this word is capital if it has more than one letter, like "Google".
Otherwise, we define that this word doesn't use capitals in a right way.

这是我提交的函数:(有六个样例不能通过,我觉得貌似没有错误)
bool detectCapitalUse(char* word) {
bool j;
int tag=0,i;
int count;
count=strlen(word);
if((word[1]>='A')&&(word[1]<='Z'))
{
for(i=2;i<count-1;i++)
{
if(('A'<=word[i])&&(word[i]<='Z'))
{
tag=tag+1;
}
}
if((tag!=0)&&(tag!=count-3)) j=false;
else j=true;
}
else
{
for(i=1;i<count-1;i++)
{
if(word[i]<='Z') {j=false;return j;}
}
j=true;
}
return j;


}
...全文
660 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
may_Xia 2017-04-20
  • 打赏
  • 举报
回复
原来真的是输入格式问题,天……
may_Xia 2017-04-20
  • 打赏
  • 举报
回复
能麻烦各位大神看下我的代码,到底哪里出错了吗?
自信男孩 2017-04-20
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>
#include <stdbool.h>


bool detect_capital_use (char *word);
bool check_word(char *word);

int main(void)
{
    char word[32];

    while (1) {
        printf("Please input a word: ");
        scanf("%s", word);

        if (!check_word(word)) {
            printf("Not a word!\n");
            continue;
        }
        if (strcmp(word, "exit") == 0)
            break;
        if (detect_capital_use(word))
            printf("the word of %s's captial is right used!\n", word);
        else
            printf("the word of %s's captial is not right used!\n", word);
    }

    return 0;
}

bool check_word(char *word)
{
    int i;
    bool flag = true;
    int len = strlen(word);

    for (i = 0; i < len; i++)
        if (! ((word[i] >= 'a' && word[i] <= 'z') || (word[i] >= 'A' && word[i] <= 'Z'))) {
            break;
        }
    if (i < len)
        flag = false;

    return flag;
}


bool detect_capital_use (char *word)
{
    bool flag = true;
    int i;
    int len = strlen(word);

    if (word[0] >= 'A' && word[0] <= 'Z') {
        if (word[1] >= 'A' && word[1] <= 'Z') {
            for (i = 2; i < len; i++) {
                if (word[i] >= 'a' && word[i] <= 'z') {
                    flag = false;
                    break;
                }
            }
        }
    } else if (word[0] >= 'a' && word[0] <= 'z') {
        for (i = 1; i < len; i++)
            if (word[i] >= 'A' && word[i] <= 'Z') {
                flag = false;
                break;
            }
    } else {
        flag = false;
    }

    return flag;
}

赵4老师 2017-04-20
  • 打赏
  • 举报
回复
边界条件 输入输出格式 ……
may_Xia 2017-04-20
  • 打赏
  • 举报
回复
还有一个557. Reverse Words in a String III(也是自己觉得找不到错误..不能通过) 题目:Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Input: "Let's take LeetCode contest" Output: "s'teL ekat edoCteeL tsetnoc" 我提交的函数: char* reverseWords(char* s) { int i,k=0,t,j; char c; int a=strlen(s); for(i=1;i<a;i++) { k++; if(s[i]==' '|s[i]=='\"') { if(k%2==0) t=k/2; else t=(k-1)/2+1; for(j=1;j<t;j++) { c=s[i-k+j]; s[i-k+j]=s[i-j]; s[i-j]=c; } if(s[i]=='\"') break; k=0; } } return s; }

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧