Re: [问题] 字串倒转 - 只倒转元音

楼主: EdareuqSam (Sam)   2016-05-11 15:08:13
※ 引述《EngRookie (EngRookie)》之铭言:
: 大家好,小弟又来打扰各位,小弟有两个问题如下:
: 1. 在不新增副函式的引数 char *returnStr 是否有其他办法 return 字串回去?
你的 returnStr 根本没用,不信你把它移除看看。
: 2. 我的 malloc 是建立在副函式里面并且在最后 return 回去,我该如何 free(pointer)呢?
在 main 里面 free(returnStr),
因为 returnStr 已经拿到 malloc 得到的内存位址。
: 程式码如下:
: #include "stdafx.h"
: #include <stdio.h>
: #include <stdlib.h>
: #include <stdbool.h>
: bool isVowels(char *s)
^
写错了
: {
: if (s >='A'&& s <='Z')
: s += 'a' - 'A';
: return s == 'a' || s == 'e' || s == 'i' || s == 'o' || s == 'u';
: }
: char* reverseVowels(char* s, char *returnStr)
^^^^^^^^^^^^^^^^^
删掉
: {
: int L = 0, R = strlen(s) - 1;
: char tempChar = '0';
: char *Array = (char *)malloc(strlen(s)*sizeof(char));
^
再补个 + 1,不然字串结尾字符 '\0' 会溢出到合法内存空间之外。
: for (int i = 0; i < strlen(s); i++)
: Array[i] = *(s + i);
: while (L < R)
: {
: while (!isVowels(Array[L]))
: L++;
: while (!isVowels(Array[R]))
: R
作者: EngRookie (EngRookie)   2016-05-11 15:21:00
谢谢您 : )

Links booklink

Contact Us: admin [ a t ] ucptt.com