简体   繁体   English

使用递归 function 将小写转换为大写并执行 while

[英]converting lower case to upper case using recursive function and do while

#include<iostream>
using namespace std;


void recursiveToLower(char str[])
{
  do  {
    str[0] = toupper( str[0] ); 
    recursiveToLower( str+1 ); 
  }
  while ( str[0] != '\0' );
}

int main()
 {
 
  char str[];
 cin.get(str,80)
 cout<<str;
   recursivetoLower(str);
 
}

I have been trying to convert lower case to upper case using recursive function and do while loop as far as I know the syntax is fine but code is not working need help to compile it while compiling the code it is giving me some errors.我一直在尝试使用递归 function 将小写字母转换为大写字母并执行 while 循环,据我所知语法很好,但代码不起作用需要帮助来编译它给我一些错误的代码。

There is no use of the 'do while' loop if you want to use recursion.如果要使用递归,则不使用“do while”循环。 Either of them can be used to solve the problem.它们中的任何一个都可以用来解决问题。 Also, there are few other errors as well, like you have not provided the size of the char array, which is required at the time of array declaration.此外,还有一些其他错误,例如您没有提供 char 数组的大小,这是在数组声明时需要的。 And also you might have mistakenly called for the wrong function 'recursivetoLower(str)' though the actual function you have created is 'recursiveToLower(char str[])'.而且您可能错误地调用了错误的 function 'recursivetoLower(str)' 虽然您创建的实际 function 是 'recursiveToLower(char str[])'。 Remove the loop and insert a base condition and it should work.删除循环并插入基本条件,它应该可以工作。 See the following code for reference.请参阅以下代码以供参考。

void recursiveToLower(char str[])
{
    if(str[0] == '\0'){
        return;
    }
    str[0] = toupper( str[0] );
    recursiveToLower( str+1 );
}

int main(){
    char str[80];
    cin.get(str,80);
    cout<<str << endl;
    recursiveToLower(str);
    cout << str;
}

You either need iteration or recursion but not both!您需要迭代或递归,但不是两者都需要!

Recursive:递归:

void recursiveToLower(char str[])
{
 
   if(str[0] == '\0') return; //Base Case
    str[0] = tolower( str[0] ); 
    recursiveToLower( str+1 ); 
 
}

Iterative:迭代:

void iterativeToLower(char str[])
{
  do  {
    str[0] = tolower( str[0] ); 
    str += 1;
 }  while ( str[0] != '\0' );
}

You don't need to use a while loop.您不需要使用 while 循环。 Just need to keep track of the state and call it recursively.只需要跟踪 state 并递归调用它。

#include<bits/stdc++.h>
using namespace std;

string recursive(string x, int i){
    if(i >= x.size()) return x;
    
    x[i] = toupper(x[i]);
    x = recursive(x, i+1);
    
    return x;
}

int main() {
    // your code goes here
    string x = "Convert to Upper";
    
    cout<<recursive(x, 0);
    return 0;
}

Let's see one by one the things that are wrong with your code.让我们一一看看你的代码有什么问题。 Here's the fixed code,这是固定代码,

#include <iostream>
#include <cctype>
using namespace std;

void recursiveToLower(char str[])   
{
    if(str[0] != '\0')  //replaced while loop with simple if 
    {
        str[0] = tolower(str[0]);   //probably should be tolower as the function name suggests
        recursiveToLower(str + 1);
    }
    else 
    {
        return; //break condition for recursion
    }
}

int main()
{
    const unsigned SZ = 100;
    char str[SZ];   //In C++ array size cannot be dynamic , you did not specify array size
    cin >> str;
    recursiveToLower(str);
    cout << str <<endl;
}

Let's start with, loop and recursion don't go together for a task like this.让我们从循环和递归开始,不要将 go 一起用于这样的任务。 This task could be done with loop or recursion, their combination which you have used don't make much sense, think about it.这项任务可以通过循环或递归来完成,您使用的它们的组合没有多大意义,请考虑一下。

Second, in C/C++, array size should be predefined.其次,在 C/C++ 中,应该预定义数组大小。

I have added some helpful comments in the corrected code, if you are having problem in some part, you may ask in the comments.我在更正的代码中添加了一些有用的注释,如果您在某些部分遇到问题,您可以在注释中提问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM