练习魔术数/删除元音

时间:2017-04-19 05:09:08

标签: c

我正在尝试完成两项练习,目前我正面临练习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;
}

2 个答案:

答案 0 :(得分:0)

你可以用这种方式编写你的removeVowels函数 -

ActivationUri

在这里,我们不再寻找元音,而是在看到新元音时向后移动,而是创建另一个数组。并且只将非元音字符复制到其中。最后,我们将临时字符串复制回原始字符串。

答案 1 :(得分:0)

尝试打印arrmagic的地址,可能NULLmagic变量重叠。