I have written a program that converts a sentence into a lowercase or uppercase
ID: 3621801 • Letter: I
Question
I have written a program that converts a sentence into a lowercase or uppercase (can contain uppercase, lowercase letters, blank spaces, digits & symbols).
The program works perfect, except my problem is that i want to use a user-defined function called
1.void convert_to_upper(char*)
2.void convert_to_lower(char*)
After the program initially asks the user to insert a sentence, the program displays the following:
Convert to [U]ppercase or [L]owercase?
if the user selects:
U(uppercase or lowercase): The program calls a user-defined function void convert_to_upper(char*) that accepts string pointer and converts the full sentence to uppercase characters using the built-in function toupper(char).
L(uppercase or lowercase): The program calls a user-defined function void convert_to_lower(char*) that accepts string pointer and converts the full sentence to lowercase characters using the built-in function tolower(char).
After the execution of the chosen function, the program displays the modified sentence.
-------------------------------------------------------------------------------------------------------------------------------
MY CODE (WORKS)
#include<iostream>
using namespace std;
int main()
{
//Declaring variables. I named my string 'sentence'.
char sentence[100];
char option;
//string input
cout<<"Please insert the sentence:"<<endl;
cin.getline(sentence,100);
//option input
cout<<endl;
cout<<"Convert to [U]ppercase or [L]lowercase?"<<endl;
cin>>option;
//validate for lowercase
if (option=='L'||option=='l')
{
for(int i=0;sentence[i]!='';i++)
{
sentence[i] = tolower(sentence[i]);
}
cout<<endl;
cout<<"The string after conversion is: "<<sentence<<endl;//display string after lowercase conversion
}
//validate for uppercase
if (option=='U'||option=='u')
{
for(int i=0;sentence[i]!='';i++)
{
sentence[i]=toupper(sentence[i]);
}
cout<<endl;
cout<<"The string after conversion is: "<<sentence<<endl;//display string after uppercase conversion
}
return 0;
}
-------------------------------------------------------------------------------------------------------------------------------
Example Screenshot:
--------------------------------------------------------------------------------------------------------------------------------
Please help in fixing my code, it works fine but i want to apply the void convert(char*) part, and can't figure out how to to use it correctly in theprogram code.
Explanation / Answer
Sorry, when pasting the solution last time it lopped off some code for some reason. Let's try this again.
#include <iostream>
using namespace std;
// converts a character array to all lowercase.
void convert_to_upper(char *data)
{
for( int i=0; data[i]!=''; i++ )
{
data[i] = tolower(data[i]);
}// for
}// convert_to_upper()
// converts a character array to all uppercase.
void convert_to_lower(char *data)
{
for(int i=0; data[i]!=''; i++)
{
data[i]=toupper(data[i]);
}
}// convert_to_lower()
int main()
{
//Declaring variables. I named my string 'sentence'.
char sentence[100];
char option;
//string input
cout<<"Please insert the sentence:"<<endl;
cin.getline(sentence,100);
//option input
cout<<endl;
cout<<"Convert to [U]ppercase or [L]lowercase?"<<endl;
cin>>option;
//validate for lowercase
if (option=='L'||option=='l')
{
convert_to_lower(sentence);
cout<<endl;
cout<<"The string after conversion is: "<<sentence<<endl;//display string after uppercase conversion
}
//validate for uppercase
if (option=='U'||option=='u')
{
convert_to_lower(sentence);
cout<<endl;
cout<<"The string after conversion is: "<<sentence<<endl;//display string after uppercase conversion
}
return 0;
}// main()