Given a string s
, return the string after replacing every uppercase letter with the same lowercase letter.
Example 1:
Input: s = "Hello " Output: "hello "
Example 2:
Input: s = "here " Output: "here "
Example 3:
Input: s = "LOVELY " Output: "lovely "
Constraints:
1 <= s.length <= 100
s
consists of printable ASCII characters.program main
implicit none
character(len=100) :: s
s = "Hello "
call solve(s)
write (*,*) s
s = "here "
call solve(s)
write (*,*) s
s = "LOVELY "
call solve(s)
write (*,*) s
contains
subroutine solve(s)
implicit none
character(len=*), intent(inout) :: s
integer :: i
do i = 1, len_trim(s)
if (s(i:i) >= 'A' .and. s(i:i) <= 'Z') then
s(i:i) = char(iachar(s(i:i)) + 32)
end if
end do
end subroutine solve
end program main
hello here lovely
! This is a full Fortran script with a valid main entry point that solves the problem
program main
implicit none
! Declare variables
character(len=100) :: s
character(len=100) :: result
! Read input from stdin
read(*,*) s
! Replace uppercase letters with lowercase letters
result = adjustl(s)
do i = 1, len(s)
if (ichar(s(i:i)) >= 65 .and. ichar(s(i:i)) <= 90) then
result(i:i) = char(ichar(s(i:i)) + 32)
end if
end do
! Print output to stdout
write(*,*) result
end program main
temp.f95:16:4: 16 | do i = 1, len(s) | 1 Error: Symbol āiā at (1) has no IMPLICIT type
def toLowerCase(s: str) -> str:
return s.lower()
The algorithm goes through each character in the input string and checks if the character is an uppercase letter. If it is, it converts the letter to lowercase, otherwise it keeps the character unchanged. In C++ and Java, we use a loop to go through each character and update it in-place. In Python, the built-in str.lower()
function is used. In JavaScript, we need to create a new string by concatenating the resulting characters. For each language, the modified string is returned.
#include <string>
std::string toLowerCase(std::string s) {
for (char& c : s) {
if (c >= 'A' && c <= 'Z') {
c = c - 'A' + 'a';
}
}
return s;
}
The algorithm goes through each character in the input string and checks if the character is an uppercase letter. If it is, it converts the letter to lowercase, otherwise it keeps the character unchanged. In C++ and Java, we use a loop to go through each character and update it in-place. In Python, the built-in str.lower()
function is used. In JavaScript, we need to create a new string by concatenating the resulting characters. For each language, the modified string is returned.