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.Charmodule, which provides functions for character manipulation, includingtoUpper.
- Imports the
-
main = do- Defines the
mainfunction, 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
namevariable.
- 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 toUpperto convert each character innameto uppercase. toUpperis a function fromData.Charthat converts individual characters to uppercase.mapappliestoUpperto each character in the string.putStrLnthen 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