Haskell Program to convert string to upper case
Program
import Data.Char
main = do
putStrLn "Enter your name:"
name <- getLine
putStrLn "Converted to upper case: "
putStrLn $ map toUpper name
This Haskell program converts a user-input string to uppercase and prints the result.
-
import Data.Char
- Imports the
Data.Char
module, which provides functions for character manipulation, includingtoUpper
.
- Imports the
-
main = do
- Defines the
main
function, which handles input and output operations.
- Defines the
-
putStrLn "Enter your name:"
- Prints a prompt asking the user to enter a name.
-
name <- getLine
- Reads user input as a string and stores it in the
name
variable.
- Reads user input as a string and stores it in the
-
putStrLn "Converted to upper case: "
- Prints a message indicating that the string will be converted to uppercase.
-
putStrLn $ map toUpper name
- Uses
map toUpper
to convert each character inname
to uppercase. toUpper
is a function fromData.Char
that converts individual characters to uppercase.map
appliestoUpper
to each character in the string.putStrLn
then prints the modified string.
- Uses
Output
$ ghc convert_toupper.hs
[1 of 1] Compiling Main ( convert_toupper.hs, convert_toupper.o )
Linking convert_toupper ...
$ ./convert_toupper
Enter your name:
oodlescoop
Converted to upper case:
OODLESCOOP