You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1.9 KiB

atoiprog

WARNING! VERY IMPORTANT!

For this exercise a function will be tested with the exam own main. However the student still needs to submit a structured program:

This means that:

  • The package needs to be named package main.
  • The submitted code needs one declared function main(func main()) even if empty.
  • The function main declared needs to also pass the Restrictions Checker(illegal functions tester). It is advised for the student to just empty the function main after its own testings are done.
  • Every other rules are obviously the same than for a program.

Instructions

  • Write a function that simulates the behaviour of the Atoi function in Go. Atoi transforms a number represented as a string in a number represented as an int.

  • Atoi returns 0 if the string is not considered as a valid number. For this exercise non-valid string chains will be tested. Some will contain non-digits characters.

  • For this exercise the handling of the signs + or - does have to be taken into account.

  • This function will only have to return the int. For this exercise the error result of atoi is not required.

Expected function

func Atoi(s string) int {

}

Usage

Here is a possible program to test your function :

package main

import (
	"fmt"
)

func main() {
	s := "12345"
	s2 := "0000000012345"
	s3 := "012 345"
	s4 := "Hello World!"
	s5 := "+1234"
	s6 := "-1234"
	s7 := "++1234"
	s8 := "--1234"

	n := Atoi(s)
	n2 := Atoi(s2)
	n3 := Atoi(s3)
	n4 := Atoi(s4)
	n5 := Atoi(s5)
	n6 := Atoi(s6)
	n7 := Atoi(s7)
	n8 := Atoi(s8)

	fmt.Println(n)
	fmt.Println(n2)
	fmt.Println(n3)
	fmt.Println(n4)
	fmt.Println(n5)
	fmt.Println(n6)
	fmt.Println(n7)
	fmt.Println(n8)
}

And its output :

student@ubuntu:~/[[ROOT]]/test$ go build
student@ubuntu:~/[[ROOT]]/test$ ./test
12345
12345
0
0
1234
-1234
0
0
student@ubuntu:~/[[ROOT]]/test$