2017-04-19 72 views
-2

我试图完成两个练习和我目前正在与运动2和4练习幻数/删除元音

在练习2中面临的问题的问题是,当魔印在第一次它的价值42,一旦输入长度为16的字符串,魔法变为0,但是如果长度小于16,那么魔法保持42。为什么?

在练习4中我试图添加一个功能到这个程序中,删除所有的元音,然后输出删除的数量和新的字符串。但是我不确定我是否在正确的轨道上。

代码:

#include <stdio.h> 
#include <learncs.h> 

// FORWARD DECLARATIONS 
void exercise2(void); 
void exercise3(void); 
void exercise4(void); 
int stringLength(char arr[]); 
int countSpaces(char arr[]); 
void removeVowels(char arr[]); 


// main() 
// Do not change this function at all. 
int main(int argc, char * argv[]) 
{ 
exercise2(); 
exercise3(); 
exercise4(); 
printf("\n"); 
return 0; 
} 


void exercise2(void) 
{ 
char arr[16]; 
int  magic = 42; 

printf("\n--------------------\n"); 
printf("EXERCISE 2\n"); 
printf("--------------------\n\n"); 

// Print out the magic value 
printf("magic = %d\n", magic); 

// Prompt for string input 
printf("Enter a character string: "); 

// Retrieve up to 16 characters (plus the null terminator) 
getString(arr, 16 + 1); 
printf("The length of string [%s] is %d\n", arr, stringLength(arr)); 

// Print out the magic value again 
printf("magic = %d\n", magic); 

/* 
    Provide the Exercise 2a explanation here, in this comment: 

    When the string is equal to 16 characters magic becomes 0. When the string is not equal to 16 characters 
    magic stays as 42. Im not sure why... 
*/ 

} 


void exercise3(void) 
{ 
int   spaces; 
char  string[] = 
    "This is a test of the emergency broadcasting system. This is only a test."; 

printf("\n--------------------\n"); 
printf("EXERCISE 3\n"); 
printf("--------------------\n\n"); 

// Count the number of spaces in the string. 
spaces = countSpaces(string); 

// The original countSpaces() function you are given simply returns -1. 
// If it still does that, it just means that you haven't yet implemented 
// the countSpaces() function according to the Exercise 2 instructions. 
if (spaces == -1) 
{ 
    printf("This exercise has not been completed yet.\n"); 
} 
else 
{ 
    printf("The number of spaces in [%s] is %d\n", string, spaces); 
} 
} 


void exercise4(void) 
{ 
int   removed; 
char  string[] = 
    "This is a test of the emergency broadcasting system. This is only a test."; 

printf("\n--------------------\n"); 
printf("EXERCISE 4\n"); 
printf("--------------------\n\n"); 

// Count the number of spaces in the string. 
removed = removeVowels(string); 

// The original removeVowels() function you are given simply returns -1. 
// If it still does that, it just means that you haven't yet implemented 
// the countSpaces() function according to the Exercise 2 instructions. 
if (removed == -1) 
{ 
    printf("This exercise has not been completed yet.\n"); 
} 
else 
{ 
    printf("%d vowels were removed, yielding [%s]\n", removed, string); 
} 
} 


/** 
* Calculate the length of a character string 
* 
* @param arr 
* The address of the first element of an array of characters containing 
* the string. 
* 
* @return 
* The number of characters in the string, not including the string's 
* null terminator. 
*/ 
int stringLength(char arr[]) 
{ 
int  len; 

// Assume initially that the array is length 0. 
len = 0; 

// Look at each element of the array. If we find something other than 
// the null terminator, count this character by incrementing the length 
// variable. 
while (arr[len] != '\0') 
{ 
    // This character wasn't the null terminator, so increment the length 
    ++len; 
} 

// Give 'em the calculated string length 
return len; 
} 


/** 
* Count the number of space characters in a string. 
* 
* @param arr 
* The address of the first element of an array of characters containing 
* the string. 
* 
* @return 
* The number of space characters in the string. 
*/ 
int countSpaces(char arr[]) 
{ 
int i, len; 
int num = 0; 

len = 0; 
while (arr[len] != '\0') 
{ 
    ++len; 
} 

for(i = 0; i < len; i++) 
{ 
    if(arr[i] == ' ') 
    { 
     (num)++; 
    } 
} 
return num; 
} 


/** 
* "Remove" each vowel from the provided character array, moving all 
* subsequent characters forward in the array to take up the space of the 
* removed vowel. Only the following characters are considered to be vowels: 
* 'a', 'e', 'i', 'o', and 'u' 
* 
* @param arr 
* The address of the first element of an array of characters containing 
* the string whose vowels are to be removed. 
* 
* @return 
* The number of vowels removed from the provided string. 
*/ 
int removeVowels(char arr[]) 
{ 
int i, len = 0, removed = 0; 

while (arr[len] != '\0') 
{ 
    ++len; 
} 

for(i = 0; i < len; i++) 
{ 
if(arr[i] == 'a' || 'e' || 'i' || 'o' || 'u') 
{ 
    removed++; 
    for(i = 0; i < len; i++) 
    { 
     arr[i] = i + 1; 
    } 
} 
} 
return removed; 
} 
+1

_ //检索最多16个字符(加上空终止符) getString(arr,16 + 1); _但是'arr'的大小是** 16 **。 – BLUEPIXY

+0

'getString()'在哪里? –

+0

@SouravGhosh可能是*#的一部分包括 *我找不到任何文档。 –

回答

0

你可以写你removeVowels运作这种方式 -

int removeVowels(char arr[]) { 
    char *temp = malloc(strlen(arr) + 1); 
    int i; 
    int total = 0; 
    int pos = 0; 
    for (i = 0; arr[i] != '\0'; i++) { 
     if (arr[i] == 'a' || arr[i] == 'e' || arr[i] == 'o' || arr[i] == 'i' || arr[i] == 'u') 
      total++; 
     else 
      temp[pos++] = arr[i]; 
    } 
    temp[pos] = '\0'; 
    strcpy(arr, temp); 
    free(temp); 
    return total; 
} 

这里,而不是寻找元音和移回来时,我们看到了一个新的,我们创建另一个数组。只复制非元音字符。最后,我们将临时字符串复制回原始字符串。

0

尝试打印arrmagic的地址,可能是NULL重叠在magic变量上。