【问题标题】:how to make recursive function thats put uppercase letter at first and after this '.' char如何制作将大写字母放在第一个和这个“。”之后的递归函数字符
【发布时间】:2015-06-16 04:21:21
【问题描述】:
  1. 字符串是一个句子,而不仅仅是一个单词。
  2. 必须是递归的。
  3. 在这里我如何做到这一点。第一个函数改变第一个字母。第二个函数将小写字母放在其余部分。

这是我尝试过的:

void changeWord(char *str){ 
    if (*str >= 'a' && *str <= 'z')
        *str  = *str - 32;   //stop condition.
    changeRest(str+1);
}


void changeRest(char *str){
    if (*str !=0){   //STOP CONDITION.
        if (*str >= 'A' && *str <= 'Z'){  //if its upper.
            *str  = *str +32; //change it to lower
        }
        changeRest(str+1); //call the new string -1.
    }
    return;
}

【问题讨论】:

  • 为什么不是迭代解决方案? (无论如何,尾递归都会被编译成迭代代码,所以递归根本不会对性能产生任何影响,但看起来很丑:))
  • 注意:不要使用*str +32,而是使用*str - 'A' + 'a'。避免使用幻数 32。
  • 它没有解决问题。我需要更正完整的句子,只有在之后。我需要输入一个大写字母。
  • 您想在字符串中添加句点,还是将其最后一个字符更改为句点?
  • 请澄清您的问题。您想知道如何解决单个单词的大小写问题,还是解析和纠正整个句子?您的代码示例似乎非常关注一个单词,但您对响应的反对意味着后者。

标签: c string recursion uppercase lowercase


【解决方案1】:

你需要一个额外的参数给你的递归函数来告诉它它是否在句子的开头。将您的代码组合成一个函数而不是两个函数更有意义。例如:

void capitalize(char *str, int sentence_start){
    if (*str == '\0') {   //STOP CONDITION.
        return;
    } else if (*str == '.') {
        sentence_start = 1;
    } else if (*str >= 'a' && *str <= 'z') {
        if (sentence_start) {
            *str = *str - 'a' + 'A';
            sentence_start = 0;
        }
    } else if (*str >= 'A' && *str <= 'Z') {
        if (sentence_start) {
            sentence_start = 0;
        } else {
            *str = *str - 'A' + 'a';
        }
    }
    capitalize(str + 1, sentence_start);
}

如果第一个字母要大写,则对该函数的顶级调用将传递sentence_start非零;如果您不想手动执行此操作,您可以编写一个包装函数来为您执行此操作。

当然,上面假设大写和小写字母都出现在一个连续的字符代码块中,大小相同,顺序对应,以'a'(分别为'A')开头和'z '(分别是'Z')在最后。那是相当以美国为中心的,但这似乎是您的要求所针对的。

【讨论】:

  • 避免 ctype.h 没什么大不了的。主要是把递归算法的结构弄对。答案已更新。
  • 非常感谢它的工作。最后 !!谢谢你和所有其他人!
【解决方案2】:

ctype.h 中的 toupper 和 tolower 函数通常是这样实现的:

char tolower (char ch)
{
  if ((ch >= 'A') && (ch <= 'Z')) 
  {
    ch = (ch - 'A') + 'a';
  }

  return ch;
}


char toupper (char ch)
{
  if ((ch >= 'a') && (ch <= 'z'))
  {
    ch = (ch - 'a') + 'A';
  }

  return ch;
}

【讨论】:

  • 是的,这是最简单的部分。但是我如何在整个字符串上运行?
【解决方案3】:

这可以满足您的所有要求。

假设:

  • 提供的缓冲区包含有效的 C 样式字符串
  • 提供的指针不为空。
  • 字符串中出现句点严格意味着句子的结尾。
  • 从上面看,没有嵌入的浮点数。因此“pi is about 3.14”是无效的(尽管这应该处理它)。
  • 字符集是原生 C 字符集(ASCII,7 位)

代码,包括助手

// Keep all helpers as functions to avoid side effects of calls
// such as toupper(text++).

unsigned char toupper(unsigned char c)
{
    return (c >= 'a' && c <= 'z') ? c - ('A' - 'a') ? c;
}

// All characters in the ranges [0,' '] and [0x7f,inf) are either
// whitespace, control characters, or otherwise non-printable.
int iswhitespace(unsigned char c)
{
    return (c <= ' ' || c >= 0x7f)
}

int isalpha(unsigned char c)
{
    unsigned char uc = toupper(c);
    return uc >= 'A' && uc <= 'Z';
}

// Note that identifiers beginning with "str" are reserved 
// by the standard library.

void Capitalize(unsigned char *text)
{
    // Base condition: at the end of the string
    if (! *text)
        return;

    // At the beginning of a sentence. Capitalize the first char.
    *text = toupper(*text);

    // Find the next sentence and recurse
    for (text++; *text && *text != '.'; text++);
    for (text++; *text && iswhitespace(*text); text++);
    Capitalize(text);
}

【讨论】:

    【解决方案4】:

    迭代函数在这里可以很好地工作。 这是一个双重递归解决方案。

    #include <string.h>
    
    int my_isalpha(char ch) {
      return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
    }
    
    char my_toupper(char ch) {
      if (ch >= 'a' && ch <= 'z') {
        ch = ch - 'a' + 'A';
      }
      return ch;
    }
    
    int EndOfSentence(int ch) {
      return !!strchr(".?!\n\v\r", ch);
    }
    
    void Sentence_Case(char *src);
    
    static void Normal_Case(char *src) {
      while (*src) {
        if (EndOfSentence(*src)) {
          Sentence_Case(src + 1);
          return;
        }
        src++;
      }
    }
    
    void Sentence_Case(char *src) {
      while (*src) {
        if (my_isalpha(*src)) {
          *src = my_toupper(*src);
          Normal_Case(src + 1);
          return;
        }
        src++;
      }
    }
    
    #include <stdio.h>
    int main(void) {
      char s[] = "good job. sir";
      Sentence_Case(s);
      puts(s);
      return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-10
      • 2022-01-09
      • 2013-06-11
      • 2016-03-12
      • 1970-01-01
      • 2014-11-26
      • 1970-01-01
      • 2020-04-29
      相关资源
      最近更新 更多