Home > database >  How to make this upper lower case c program work?
How to make this upper lower case c program work?

Time:11-14

I am trying to make this app which converts upper case characters of a string to lower case and vice versa.

But when i run the code it displays a really weird output

The code i wrote:

#include <iostream>
#include <string.h>

std::string toggle(std::string str)
{
    #define maxsize 100
    if (sizeof(str)>maxsize)
    {
        std::cout << "Size of string is too big!" << std::endl;
        exit(1);
    }
    else
    {
        for (int i = 0 ; i<=sizeof(str) ; i  )
        {
            if (isupper(str[i]))
            {
                std::cout << "UPPER CASE CONVERTED TO LOWER CASE "<< tolower(str[i]) <<"" << std::endl;
            }
            else
            {
                std::cout << "LOWER CASE CONVERTED TO UPPER CASE "<< toupper(str[i]) <<"" << std::endl;
            }

        }
    }
}


int main()
{
    toggle("Hello This Is A Test");
}

The output

UPPER CASE CONVERTED TO LOWER CASE 104
LOWER CASE CONVERTED TO UPPER CASE 69
LOWER CASE CONVERTED TO UPPER CASE 76
LOWER CASE CONVERTED TO UPPER CASE 76
LOWER CASE CONVERTED TO UPPER CASE 79
LOWER CASE CONVERTED TO UPPER CASE 32
UPPER CASE CONVERTED TO LOWER CASE 116
LOWER CASE CONVERTED TO UPPER CASE 72
LOWER CASE CONVERTED TO UPPER CASE 73
LOWER CASE CONVERTED TO UPPER CASE 83
LOWER CASE CONVERTED TO UPPER CASE 32
UPPER CASE CONVERTED TO LOWER CASE 105
LOWER CASE CONVERTED TO UPPER CASE 83
LOWER CASE CONVERTED TO UPPER CASE 32
UPPER CASE CONVERTED TO LOWER CASE 97
LOWER CASE CONVERTED TO UPPER CASE 32
UPPER CASE CONVERTED TO LOWER CASE 116
LOWER CASE CONVERTED TO UPPER CASE 69
LOWER CASE CONVERTED TO UPPER CASE 83
LOWER CASE CONVERTED TO UPPER CASE 84
LOWER CASE CONVERTED TO UPPER CASE 0
LOWER CASE CONVERTED TO UPPER CASE 0
LOWER CASE CONVERTED TO UPPER CASE 0
LOWER CASE CONVERTED TO UPPER CASE 0
LOWER CASE CONVERTED TO UPPER CASE -73
LOWER CASE CONVERTED TO UPPER CASE -125
LOWER CASE CONVERTED TO UPPER CASE -97
LOWER CASE CONVERTED TO UPPER CASE 34
UPPER CASE CONVERTED TO LOWER CASE 119
UPPER CASE CONVERTED TO LOWER CASE 98
LOWER CASE CONVERTED TO UPPER CASE 0
LOWER CASE CONVERTED TO UPPER CASE 0
LOWER CASE CONVERTED TO UPPER CASE -48

Why is it displaying integers instead of a string? Am i doing something wrong?

I can see it is converting the upper case characters to lower case and back but why is it not displaying strings?

CodePudding user response:

First i have added a return inside the toggle function. Second, you can find out the length/size of a std::string using the size() member function. Using these two modifications your program would look like:

#include <iostream>
#include <string.h>
 #define maxsize 100
std::string toggle(std::string str)
{
   
    if ( str.size() > maxsize)
    {
        std::cout << "Size of string is too big!" << std::endl;
        
    }
    else
    {
        for (int i = 0 ; i < str.size() ; i  ) //note here i have removed <= and instead used <
        {
            if (isupper(str.at(i)))
            {
                str.at(i) = tolower(str.at(i));
                std::cout << "UPPER CASE CONVERTED TO LOWER CASE "<< str.at(i) <<" " << std::endl;
                
            }
            else
            {
                str.at(i) = toupper(str.at(i));
                std::cout << "LOWER CASE CONVERTED TO UPPER CASE "<< str.at(i) <<" " << std::endl;
            }

        }
        
    }
    return str; //added this return
}


int main()
{
    toggle("Hello This Is A Test");
}

The output of the above program is:

UPPER CASE CONVERTED TO LOWER CASE h 
LOWER CASE CONVERTED TO UPPER CASE E 
LOWER CASE CONVERTED TO UPPER CASE L 
LOWER CASE CONVERTED TO UPPER CASE L 
LOWER CASE CONVERTED TO UPPER CASE O 
LOWER CASE CONVERTED TO UPPER CASE   
UPPER CASE CONVERTED TO LOWER CASE t 
LOWER CASE CONVERTED TO UPPER CASE H 
LOWER CASE CONVERTED TO UPPER CASE I 
LOWER CASE CONVERTED TO UPPER CASE S 
LOWER CASE CONVERTED TO UPPER CASE   
UPPER CASE CONVERTED TO LOWER CASE i 
LOWER CASE CONVERTED TO UPPER CASE S 
LOWER CASE CONVERTED TO UPPER CASE   
UPPER CASE CONVERTED TO LOWER CASE a 
LOWER CASE CONVERTED TO UPPER CASE   
UPPER CASE CONVERTED TO LOWER CASE t 
LOWER CASE CONVERTED TO UPPER CASE E 
LOWER CASE CONVERTED TO UPPER CASE S 
LOWER CASE CONVERTED TO UPPER CASE T 

as can be seen here.

  • Related