-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMain.go
82 lines (77 loc) · 1.71 KB
/
Main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//Implement various string function in Golang [substring, replace, length, reverse]
package main
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
loop := true
fmt.Println("Enter the String: ")
in := bufio.NewReader(os.Stdin)
value, _ := in.ReadString('\n')
value = strings.Replace(value, "\n", "", -1)
for loop {
fmt.Println("Choose an Operation:")
fmt.Println("1. Sub String, 2. Replace, 3. Length, 4. Reverse, 5. Exit")
var a int
fmt.Scan(&a)
switch a {
case 1:
fmt.Printf("Enter Starting position for Sub String: ")
var start int
fmt.Scan(&start)
fmt.Printf("Enter Ending position for Sub String: ")
var end int
fmt.Scan(&end)
subString(value, start, end)
case 2:
replace(value)
case 3:
length(value)
case 4:
reverse(value)
case 5:
loop = false
default:
fmt.Println("Invalid Input")
}
}
}
func subString(value string, start int, end int) string {
runes := []rune(value)
mySubString := string(runes[start:end])
fmt.Println("Your Sub String is: " + mySubString)
return string(mySubString)
}
func replace(value string) {
fmt.Printf("Enter character to be replaced: ")
var a string
fmt.Scan(&a)
fmt.Printf("Enter character to be substituted: ")
var b string
fmt.Scan(&b)
a = strings.Replace(value, a, b, -1)
fmt.Println("Your New Replaced String is: " + a)
}
func length(value string) int {
a := len(value)
fmt.Printf("Length of your String is: %v\n", a)
return a
}
func reverse(value string) string {
n := 0
rune := make([]rune, len(value))
for _, r := range value {
rune[n] = r
n++
}
rune = rune[0:n]
for i := 0; i < n/2; i++ {
rune[i], rune[n-1-i] = rune[n-1-i], rune[i]
}
output := string(rune)
fmt.Println(output)
return output
}