【问题标题】:How to write out Capital Words in String?如何在字符串中写出大写单词?
【发布时间】:2015-01-07 00:19:56
【问题描述】:

我正在使用 Visual Studio 2012。

我需要写一个句子,然后检查该句子中有多少单词以大写字母开头(A,G,Z,U ...)-好的,我做到了,现在我需要写去掉以大写字母开头的单词

例如:“嗨,请帮我解决这个问题”,我需要我的程序说: " 5 个单词以大写字母开头,这些单词是:Hi Please Help With QUESTION "

这是我已经做过的(计算以大写字母开头的单词):

#include<stdio.h>
#include<string.h>
#define lenght 20
int main(){
char sentence[lenght];
int i,n=0,num;
printf("\nWrite a sentence: ");
gets(sentence);
if(sentence[0]>='A' && sentence[0]<='Z'){
            n++;
        }
num=strlen(sentence);
for(i=0;i<num;i++){
    if(!(sentence[i]>='A' && sentence[i]<='Z' || sentence[i]>='a' && sentence[i]<='z')){
        if(sentence[i+1]>='A' && sentence[i+1]<='Z'){
        n++;
        }}}
printf("\nNum of words that start with uppercase letter is:%d \n",n);
return 0;
}

这可行,但我不知道如何写出以大写字母开头的单词,我尝试了 strcpy 但没用,我也尝试只用 printf 做,但也没有用。 请帮忙! P.S 谢谢大家的帮助!

【问题讨论】:

  • "... 以大写字母开头(A,G,Z,U...)" - 大写字母的例子?严重地?任何不知道大写字母是什么的人请举手:-)

标签: string visual-studio-2012 uppercase words sentence


【解决方案1】:

对于任何包含字母的字符位置,您可以通过查看前一个字符位置来检查它是否是单词的开头。如果那是也是一个字母,那么你不在单词的开头。

【讨论】:

  • 我知道如何检查它是否是一封信,但我不知道如何检查它是否不是一封信。我开始:if(sentence !=)...但我不知道如何继续。
  • if(sentence[i]>='A' && sentence[i]='a' && sentence[i]
  • 你可以用两种方式:一种是使用!,所以if (!(sentence[i] &gt;= 'A' &amp;&amp; sentence[i] &lt;= 'Z')),另一种是使用De Morgan's laws来反转整个测试:if (sentence[i] &lt; 'A' || sentence[i] &gt; 'Z')
【解决方案2】:

您可能应该做的第一件事是定义单词的开头是什么。只有这样你才能知道它是否是大写的。

例如,我直接(幼稚)的想法是,单词的第一个字母要么位于文本的开头,要么紧跟在某些描述的空格之后(换行符、空格、制表符等)。

如果是这种情况,只需计算您看到其中一个空白字符后紧跟一个大写字母的次数就很简单了。这种野兽的伪代码是这样的:

def countUppercasedWords (sentence):
    count = 0
    for i = 0 to sentence.lastCharIndex - 1:
        if sentence[i] in '<whitespace>' and sentence[i+1] in 'A..Z':
            count = count + 1
    return count

现在可能是我使用的定义过于简单(例如,这句话),因为我没有将左括号视为空格。

但您必须取得平衡,以免误报,例如在这句话中,YOURE 可能被视为单独的词。

也许一种可行的方法是列出可以检查的有效单词列表(即完整的字典,适当小写)。然后,对于文本中的每个位置,获取构成有效单词的最大字符数(再次小写以进行检查)并检查其中的第一个字符,然后再推进那么多字符。

如果您在当前位置无法看到有效单词,只需前进到下一个字符并继续尝试。

【讨论】:

    【解决方案3】:

    看来您使用的是 C,而不是 C++,对吧?

    您需要一个空间来存储所有这些单词。您应该使用动态分配或足够大的字符串数组。第二种方法比较简单,所以我会用它。

    #define MAX_SENTENCE_LENGTH 1024
    #define MAX_WORD_LENGTH 64
    #define MAX_WORDS 32
    
    . . .
    
    char sentence[MAX_SENTENCE_LENGTH];
    char words[MAX_WORDS][MAX_WORD_LENGTH];
    int word_count;
    char* current_char;
    size_t word_length;
    
    gets(sentence);
    current_char = sentence;
    while(1) {
        /* Skip white spaces */
        while(*current_char != '\0' && isspace(*current_char))
            current_char++;
    
        if(*current_char == '\0')
            break;
    
        if(isupper(*current_char)) {
            /* Store word in the array and increment word's count.
            word_length = 0;
    
            while(current_char[word_length] != '\0' && !isspace(current_char[word_length])) {
                words[word_count][word_length] = current_char[word_length];
                word_length++;
            }
    
            word[word_count][word_length] = '\0';
            word_count++;
        }
        else {
            /* Skip word without first upper letter */
            while(*current_char != '\0' && !isspace(*current_char))
                current_char++;
        }
    }
    . . .
    printf("%d words are found and these words are:\n", word_count);
    for(int i = 0; i < word_count; i++)
         printf("%s\n", words[i]);
    

    【讨论】:

      猜你喜欢
      • 2016-09-06
      • 2013-02-28
      • 1970-01-01
      • 2018-08-06
      • 1970-01-01
      • 1970-01-01
      • 2023-02-03
      • 2011-01-20
      • 1970-01-01
      相关资源
      最近更新 更多