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