C++ Make Lower Function

1.1k views Asked by At

I wrote a simple function in c++ to turn a string in to all lower case letters using chars and iterating through each char in the string. Can someone please explain why when I run this program in the console window, I get an output in addition to my original input if the function never references cout.

#include <iostream>
#include <cctype>
#include <cstdlib>
using namespace std;
string makelower(string text)
{
    int iter = 0;
    char cha;
    string newtext;
    while (iter < text.length())
    {
        cha = text[iter];
        cha = tolower(cha);
        newtext+=cha;
        iter++;
    }
    return(newtext);
}

int main()
{
    string a;
    cin>>a;
    a = makelower(a);
    cout<<a;
}
3

There are 3 answers

4
echen On BEST ANSWER

putchar writes a character out to stdout http://www.cplusplus.com/reference/cstdio/putchar/

0
AStopher On

The putchar function puts outputs the assigned character to stdout. Additional information on this can be found here, and you should instead simply tolower the char:

string makelower(string text)
{
    int iter = 0;
    char cha;
    while (text[iter])
    {
        tolower(text[iter]);
        iter++;
    }
return(text);
}

Making your full program like this (I've also made the code more efficient):

#include "stdafx.h"
#include <iostream>
#include <ctype.h>
#include <string>
#include <cstdlib>
using namespace std;

string makelower(string text)
{
    int iter = 0;
    while (text[iter])
    {
        tolower(text[iter]);
        iter++;
    }
    return(text);
}

int main()
{
    string a;
    cin >> a;
    a = makelower(a);
    cin >> a;
}

An #include <string> was missing which stopped the code from compiling when I tested it properly, so I added it in. Remove it if you already have it.

0
James Kanze On

There are a number of issues with your code. Perhaps the most significant one is that you haven't defined what you mean by converting to lower case; it's not always as obvious as it seems. However:

  • To iterate over the characters in a string, you use iterators, and you test for the end; in C++11, you can use a range based loop.

  • The argument to the single argument form of tolower must have a value between 0 and UCHAR_MAX; if you have a char, this is normally achieved by converting it to unsigned char.

  • If you want to return a string, then you should append the characters to it as you convert them; alternatively, you can do the changes in place by having makelower take a non-const reference. I'd recommend the former.

Taking all of this into consideration:

std::string
makeLower( std::string const& original )
{
    std::string results;
    for ( unsigned char ch : text ) {
        results += std::tolower( ch );
    }
    return results;
}

To use this function, you call it with a string, and either initialize another string with its results, or assign it to a string. Or just use it in an expression where you need a string.

std::string line;
while ( std::getline( std::cin, a ) && !line.empty() ) {
    std::cout << makeLower( a ) << std::endl;
}