Implement own function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
Example 1:
Input: "Hello Java" Output: "hello java"
Approach
Java
public class ToLowerCase {
public static void main(String[] args) {
String str = "Hello Java";
String lower = toLowerCase(str);
System.out.println(lower);
}
public static String toLowerCase(String a) {
for (int i = 0; i < a.length(); i++) {
char aChar = a.charAt(i);
if (65 <= aChar && aChar <= 90) {
char lChar = (char) ((aChar + 32));
a = a.replace(aChar, lChar);
}
}
return a;
}
}
C++
#include <bits/stdc++.h>
using namespace std;
//function to convert the
//upper case letters into
//lower case
string tolowerCase(string str)
{
for(int i=0;i<str.size();i++)
{
if(str[i]>='A'&&str[i]<='Z')
str[i]=str[i]+32;
}
return str;
}
int main()
{
string str="Hello Java";
string lower=tolowerCase(str);
cout<<lower<<"\n";
return 0;
}
No comments:
Post a Comment