对字符串中的所有单词进行倒排。
说明:
1、构成单词的字符只有26个大写或小写英文字母;
2、非构成单词的字符均视为单词间隔符;
3、要求倒排后的单词间隔符以一个空格表示;如果原字符串中相邻单词间有多个间隔符时,倒排转换后也只允许出现一个空格间隔符;
4、每个单词最长20个字母;
数据范围:字符串长度满足
对字符串中的所有单词进行倒排。
说明:
1、构成单词的字符只有26个大写或小写英文字母;
2、非构成单词的字符均视为单词间隔符;
3、要求倒排后的单词间隔符以一个空格表示;如果原字符串中相邻单词间有多个间隔符时,倒排转换后也只允许出现一个空格间隔符;
输入一行,表示用来倒排的句子
输出句子的倒排结果
I am a student
student a am I
$bo*y gi!r#l
l r gi y bo
package main import ( "bufio" "fmt" "os" "regexp" "strings" ) func reverse(s []string)string{ for i,j:=0,len(s)-1;i<j;i,j=i+1,j-1{ s[i],s[j] = s[j],s[i] } return strings.Join(s, " ") } func main() { scanner:=bufio.NewScanner(os.Stdin) scanner.Scan() s:=scanner.Text() re,_:=regexp.Compile(`\W`) arr:=re.Split(s,-1) res:=reverse(arr) fmt.Println(res) }用正则很简单
package main import ( "bufio" "fmt" "os" ) func main() { rd := bufio.NewReader(os.Stdin) str, _ := rd.ReadString('\n') var flag bool var start, end int for i := len(str) - 1; i >= 0; i-- { if isValid(str[i]) { if !flag { end = i flag = true } start = i if i == 0 { fmt.Printf("%s ", str[start:end+1]) } } else { if flag { fmt.Printf("%s ", str[start:end+1]) flag = false } } } } func isValid(c byte) bool { return (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') }
package main import ( "bufio" "fmt" "os" "strings" "unicode" ) func main() { reader := bufio.NewReader(os.Stdin) a, err := reader.ReadString('\n') a = strings.TrimSpace(a) if err != nil { return } else { for _, v := range a { cur := fmt.Sprintf("%c", v) if !unicode.IsLetter(v) && cur != " " { a = strings.ReplaceAll(a, cur, " ") } else { continue } } res := []string{} aSplit := strings.Split(a, " ") for i := len(aSplit) - 1; i > -1; i-- { res = append(res, aSplit[i]) } fmt.Println(strings.Join(res, " ")) } }
package main import ( "bufio" "fmt" "os" "strings" ) func main() { scanner := bufio.NewScanner(os.Stdin) scanner.Scan() input := scanner.Text() sb := strings.Builder{} for i := 0; i < len(input); i++ { ch := input[i] if ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z' { sb.WriteByte(ch) } else { sb.WriteByte(' ') } } words := strings.Fields(sb.String()) for i := len(words) - 1; i >= 0; i-- { fmt.Printf("%s ", words[i]) } }
package main import ( "bufio" "fmt" "os" "strings" ) func main(){ scanner := bufio.NewScanner(os.Stdin) for scanner.Scan(){ sentence := scanner.Text() // ss := strings.Split(sentence,"") ss := []rune(sentence) for i:=0;i<len(ss); i++{ if ss[i] >= 'a' && ss[i] <= 'z' || ss[i] >= 'A' && ss[i] <= 'Z'{ //fmt.Println(string(ss[i])) continue }else { ss[i] = ' ' } } str := make([]string,0) for _, v := range ss { str = append(str, string(v)) } st := strings.Join(str,"") st1 := strings.Split(st," ") for i:=len(st1);i>0;i--{ fmt.Print(st1[i-1]," ") } } }
package main import ( "bufio" "bytes" "fmt" "os" "strings" ) func isAlphabet(c byte) bool { return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' } func splitSentence(sentence string) []string { i := 0 // 需要先跳过非字母起始的字符,不然下面的循环会多添加一个空字符串 for ; i < len(sentence); i++ { if isAlphabet(sentence[i]) { break } } var result []string var word bytes.Buffer isNewWord := false for ; i < len(sentence); i++ { if isAlphabet(sentence[i]) { if isNewWord { result = append(result, word.String()) word.Reset() isNewWord = false } word.WriteByte(sentence[i]) } else { isNewWord = true } } if !isNewWord { result = append(result, word.String()) } return result } func reverseSentence(sentence string) string { words := splitSentence(sentence) var result []string for i := len(words) - 1; i >= 0; i-- { result = append(result, words[i]) } return strings.Join(result, " ") } func main() { reader := bufio.NewReader(os.Stdin) for { data, _, err := reader.ReadLine() if err != nil { break } fmt.Println(reverseSentence(string(data))) } }
package main import ( "bufio" "fmt" "os" ) func main(){ // 读取终端输入 var scanner = bufio.NewScanner(os.Stdin) scanner.Scan() var str = scanner.Text() // 逆序打印,如果非字母只打印空格 var slice []string var tempStr = "" for i:=0;i<len(str);i++ { if (str[i] >= 97 && str[i] <= 122) || (str[i] >= 65 && str[i] <= 90) { tempStr = tempStr + string(str[i]) if i == len(str) -1 { slice = append(slice, tempStr) } }else { slice = append(slice, tempStr) tempStr = "" } } for i:=len(slice)-1;i>=0;i-- { fmt.Printf("%v ",slice[i]) } }