Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我需要知道如何将c字符串从一个变量复制到另一个变量,以便在我的程序中使用第三个函数_C++_String_Function - Fatal编程技术网

C++ 我需要知道如何将c字符串从一个变量复制到另一个变量,以便在我的程序中使用第三个函数

C++ 我需要知道如何将c字符串从一个变量复制到另一个变量,以便在我的程序中使用第三个函数,c++,string,function,C++,String,Function,我试着为一个作业写三个函数 changeUp将字符串更改为大写 changeDown将字符串更改为小写 changeRev反转每个字符的大小写 每个函数都必须接受指向字符串或C字符串的指针作为参数,并且必须通过迭代输入字符串中的每个字符来进行上述更改 输入AbCd时,第一个函数应返回AbCd;第二个函数应该返回abcd;反向功能应返回aBcD 我不知道如何让最后一个函数工作——它只返回所有大写字母 我已经尝试将输入复制到一个变量来保存它-你可以看到我试图这样做的注释区域,但我真的不知道我在做

我试着为一个作业写三个函数

  • changeUp
    将字符串更改为大写
  • changeDown
    将字符串更改为小写
  • changeRev
    反转每个字符的大小写
每个函数都必须接受指向字符串或C字符串的指针作为参数,并且必须通过迭代输入字符串中的每个字符来进行上述更改

输入
AbCd
时,第一个函数应返回
AbCd
;第二个函数应该返回
abcd
;反向功能应返回
aBcD

我不知道如何让最后一个函数工作——它只返回所有大写字母

我已经尝试将输入复制到一个变量来保存它-你可以看到我试图这样做的注释区域,但我真的不知道我在做什么,我只是说实话。我在尝试复制(第行,第2行)时遇到各种各样的错误

#包括“stdafx.h”
#包括
#包括
使用名称空间std;
void changeUp(char*line)//更改为大写
{
for(int i=0;行[i];i++)
{
如果(islower(line[i])line[i]=toupper(line[i]);
}
}
void changeown(char*line)//更改为小写
{
for(int i=0;行[i];i++)
{
如果(isupper(line[i])line[i]=tolower(line[i]);
}
}
void changeRev(char*line)//反转大小写
{
for(int i=0;行[i];i++)
{
如果(isupper(line[i])line[i]=tolower(line[i]);
如果(islower(line[i])line[i]=toupper(line[i]);
}
} 
void main()
{
常量int SIZE=81;//用于字符空间
字符行[SIZE],第2行[SIZE];//字符数组

cout您就快到了。问题是您正在对同一个变量进行操作,并且每次在该变量上运行函数时都会覆盖输入
。当您运行
changeRev
时,您是在“反转”使用
changeDown
将输入字符串修改为小写后的结果。反转此字符串自然会使其全部为大写

创建一个副本是正确的做法,但是,您刚刚将参数的顺序弄错了。目标是第一个,源是第二个。然后确保只对副本,
line2
进行操作,以便用户的原始输入在
变量中保持不变,以便您可以再次使用它n稍后

strcpy_s(line2, line);
changeUp(line2);
cout << "Changed to Upper Case " << line2 << endl;/


strcpy_s(line2, line);
changeDown(line2);
cout << "Changed to Lower Case " << line2 << endl;

strcpy_s(line2, line);
changeRev(line2);
cout << "The reverse of the characters is " << line2 << endl;
strcpy_s(第2行,第2行);
换装(2号线);

cout原因是函数正在修改字符串。因此字符串从
AbCd
=>
AbCd
=>
AbCd
=>
AbCd
。在第一次调用中,您将丢失原始字符串


解决方案是每次复制一个字符串并调用副本上的函数。

包含“使用名称空间std”是不好的做法,最好使用std::cout,这适用于该名称空间中的所有函数。此外,您发送了一个指向函数的指针,因此每次函数修改字符串时,都会发送一个指针,因此在反向条件之前,您会传递所有小写字母,因此这就是为什么要获取所有大写字母。我认为类似下面的解决方案这是更好的办法

#include <iostream>
#include <string>


using namespace std;

string changeUp(string line)//Change to upper case
{
    for (int i = 0; line[i]; i++)
    {
        if (islower(line[i])) line[i] = toupper(line[i]);

    }

    return line;
}

string changeDown(string line)//Change to lower case
{
    for (int i = 0; line[i]; i++)
    {
        if (isupper(line[i])) line[i] = tolower(line[i]);
    }

    return line;
}

string changeRev(string line)//Reverse the cases
{
    for (int i = 0; line[i]; i++)
    {
        if (isupper(line[i])) line[i] = tolower(line[i]);
        else if (islower(line[i]))  line[i] = toupper(line[i]);
    }
    return line;
}

void main()
{
    const int SIZE = 81; //For character space
    char line[SIZE], line2[SIZE];//Character array

    cout << "Enter a string  of characters :" << endl;//Takestring       input from user
    cin.getline(line, SIZE);


    //strcpy_s(line, line2);

    cout << "Changed to Upper Case " << changeUp(line) << endl;//Output string in all caps


    //strcpy_s(line, line2);

    cout << "Changed to Lower Case " << changeDown(line) << endl;//Output string in lower case

    //strcpy_s(line, line2);
    cout << "The rverse of the characters is " << changeRev(line) << endl;//Output     reverse of string entered
    system("pause");
    return;
}
#包括
#包括
使用名称空间std;
字符串changeUp(字符串行)//更改为大写
{
for(int i=0;行[i];i++)
{
如果(islower(line[i])line[i]=toupper(line[i]);
}
回流线;
}
字符串转换(字符串行)//更改为小写
{
for(int i=0;行[i];i++)
{
如果(isupper(line[i])line[i]=tolower(line[i]);
}
回流线;
}
string changeRev(字符串行)//反转大小写
{
for(int i=0;行[i];i++)
{
如果(isupper(line[i])line[i]=tolower(line[i]);
如果(islower(line[i])line[i]=toupper(line[i]);
}
回流线;
}
void main()
{
常量int SIZE=81;//用于字符空间
字符行[SIZE],第2行[SIZE];//字符数组

不能使用
void main()发布代码
。这是非标准的,不能用大多数编译器编译。我现在为您解决了这个问题。同样,请不要发布不必要地使用非标准头的代码,例如
。这些头不能很容易地删除,因为代码可能取决于它们的内容,并且它们阻止读者试用您的代码。
strcpy\s(第2行,第2行)
?你知道为什么
changeRev
函数似乎不起作用吗?你知道如何通过复制输入行来解决问题吗?如果你知道如何复制一行的话?你想解决哪个问题?@Beta-我知道它为什么不起作用,我得到的是与转换函数相反的结果,但不知道如何更正这是我试图解决的问题。我在第二个学期的C++编程中,完全没有以前的经验。看起来很简单的事情对我来说不是这样。。是visual studio Win32ConsoleApplication的必选标题,我必须使用。谢谢。谢谢!我知道它必须简单,但我就是做不到。成功了!@Jesse Good-现在从中吸取教训,当你对某些事情不确定时,你应该始终使用它。使用调试器可以经常帮助其他人@J.:一个更重要的教训是尽可能独立地开发新功能。你想要复制一个字符串;你试图在一个大的、运行不正常的程序的上下文中复制一个字符串。如果你硬编码了两个字符串,试图将一个复制到另一个,然后将它们都打印出来,你就可以在两分钟内解决这个问题utes(即使文档有误,偶尔也是如此)。@Beta Poin
#include <iostream>
#include <string>


using namespace std;

string changeUp(string line)//Change to upper case
{
    for (int i = 0; line[i]; i++)
    {
        if (islower(line[i])) line[i] = toupper(line[i]);

    }

    return line;
}

string changeDown(string line)//Change to lower case
{
    for (int i = 0; line[i]; i++)
    {
        if (isupper(line[i])) line[i] = tolower(line[i]);
    }

    return line;
}

string changeRev(string line)//Reverse the cases
{
    for (int i = 0; line[i]; i++)
    {
        if (isupper(line[i])) line[i] = tolower(line[i]);
        else if (islower(line[i]))  line[i] = toupper(line[i]);
    }
    return line;
}

void main()
{
    const int SIZE = 81; //For character space
    char line[SIZE], line2[SIZE];//Character array

    cout << "Enter a string  of characters :" << endl;//Takestring       input from user
    cin.getline(line, SIZE);


    //strcpy_s(line, line2);

    cout << "Changed to Upper Case " << changeUp(line) << endl;//Output string in all caps


    //strcpy_s(line, line2);

    cout << "Changed to Lower Case " << changeDown(line) << endl;//Output string in lower case

    //strcpy_s(line, line2);
    cout << "The rverse of the characters is " << changeRev(line) << endl;//Output     reverse of string entered
    system("pause");
    return;
}