Golang is difficult to translate. .. .. (Part 1)

background

I'm translating C language to Golang, but it's quite difficult.

Iterative processing (basically for only?) Or character strings (char type is not in Golang).

So, I'm making a "program that counts the number of vowels from an input string", but the "string", "byte", and "rune" types are brilliant.

If you know a breakthrough, I would appreciate it if you could tell me.

C code

2-3.c


#include <stdio.h>

int main(void)
{
	char moziretu[100] = {};
	char boin[5] = {'a', 'i', 'u', 'e', 'o'};
	int count[5] = {};  /*Each element corresponds to the character of the array boin.(count[0]To'a'Substitute the count number of)*/

	int i;
	int j = 0;
	

	
	printf("String>>>");
	/*Enter a string*/
	scanf("%s", moziretu);

	/*Repeat until enter is loaded*/
	while(moziretu[j] != '\0'){
		for(i = 0; i < 5; i++){
			/*Count if a vowel is found in the string*/
			if(moziretu[j] == boin[i]) count[i]++;
		}
		j++;
	}

		/*Show the number of vowels*/
	for(i = 0; i < 5; i++){
		printf("%c : %d\t", boin[i], count[i]);
	}

	printf("\n");
	return 0;
}

Golang code

2-3.go


package main

import "fmt"

func main(){
	/*Input string definition*/
	var input string

	/*Array to store vowels*/
	boin := []rune{'a', 'i', 'u', 'e', 'o'}

	/*Number of counts for each vowel*/
	count := []int{0, 0, 0, 0, 0}

	/*For loops*/
	i := 0
	j := 0

	/*Character string input*/
	fmt.Printf("String>>>")
	fmt.Scanln("%s", &input)
	str := []rune(input)

	/*Repeat until enter is loaded*/
	for {
		for i = 0; i < 5; i++ {
			/*Count if a vowel is found in the string*/
			if str[j] == boin[i] {
				count[i] += 1
			}
		}
		if str[j] == '\x00' {
			break
		}
		j += 1
	}

	/*Show the number of vowels*/
	for i = 0; i < 5; i++ {
		fmt.Printf("%s : %d\n", boin[i], count[i])
	}
}

By the way, running the above Golang code causes a panic.

$ go run 2-3.go
String>>>panic: runtime error: index out of range

goroutine 1 [running]:
panic(0x4e8200, 0xc82000a0e0)
        /usr/lib/go-1.6/src/runtime/panic.go:481 +0x3e6
main.main()
        /home/yuki-f/Golang/pro2/2-3.go:28 +0x55f
exit status 2

Postscript

fixed.

package main

import "fmt"

func main(){
	/*Input string definition*/
	var input string

	/*Array to store vowels*/
	boin := []rune{'a', 'i', 'u', 'e', 'o'}

	/*Number of counts for each vowel*/
	count := []int{0, 0, 0, 0, 0}

	/*For loops*/
	i := 0
	j := 0

	/*Character string input*/
	fmt.Printf("String>>>")
	fmt.Scanf("%s", &input)
	str := []rune(input)

	/*Repeat until enter is loaded*/
	for j < len(str){
		for i = 0; i < 5; i++ {
			/*Count if a vowel is found in the string*/
			if str[j] == boin[i] {
				count[i] += 1
			}
		}
		j += 1
	}

	/*Show the number of vowels*/
	for i = 0; i < 5; i++ {
		fmt.Printf("%c : %d\n", boin[i], count[i])
	}
}

Recommended Posts

Golang is difficult to translate. .. .. (Part 1)
Binary tree traverse is difficult to understand
Introduction to PyQt4 Part 1
Re: Python lambda is useless ^ H ^ H ^ H ^ H ^ H Difficult to use
Part 1 Attempt to code mathematics (∈)
Cython is coming to AtCoder !!
Introduction to Ansible Part ③'Inventory'
Introduction to Ansible Part ④'Variable'
Ruby on Windows Clear file management that is difficult to understand
How to utilize Python with Jw_cad (Part 1 What is external transformation)