首页 > 解决方案 > 问:数组和函数作业

问题描述

坦率地说,就目前而言,我目前不太擅长编码。我真的很想完成这些功能,我只是在执行其中一些功能时遇到了麻烦。

基本上,需要创建 10 个函数(其中 6 个我已经完成?)。有一个 int main() 但是,除了需要修复的字符串之外,不需要触及 int main()。

除了 int main() 之外,我还将发布程序(主要是为了让您可以看到我的工作),以便如果有人想检查意图,他们可以看到它。尽管我已经发布了整个程序,但我真正想要关注的只有 makeUpper,以及其中没有任何内容的函数,因为我真的不明白如何将字符串复制和编辑到另一个数组中。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

void makeUpper( char orig[], char result[] )
{
/*
        int i = 0;

        while ( str[i] != '\0' )
        {
                if ( str[i] >= 'a' && str[i] <= 'z' )
                        str[i] = str[i] - 32;

                k++;
        }
*/
}

int countDigit( char str[] )
{
        int i;
        int count = 0;
        int len;

        len = strlen(str);

        for ( i = 0; i < len; i++ )
        {
                if ( i >= '0' && i <= '9' )
                        count++;
        }

        return count;
}

int onlyLetters( char orig[], char letters[] )
{
}

int countOdd( int A[], int N )
{
        int i;
        int odd = 0;

        for ( i = 0; i < N; i++ );
        {
                if ( A[i] % 2 != 0 )
                        odd++;
        }

        return odd;
}

int sumList( int A[], int N )
{
        int i;
        int sum = 0;

        for ( i = 0; i < N; i++ );
                sum = sum + A[i];

        return sum;
}

void addToEach( int A[], int N, int val )
{
        int i;

        for ( i = 0; i < N; i++ )
                A[i] + val;

        printf("%d", A[i]);
}

void copyNumList( int orig[], int N, int result[] )
{
}

// populate list A with N items, random in range L to H
void fillNumList( int A[], int N, int L, int H )
{
}

// print the list, 10 items per line
void printList( int A[], int N )
{
        int i;

        for ( i = 0; i < N; i++ )
                printf("%d ", A[i]);

        printf("\n");
}

void rept( int n, char c )
{
        int i;

        for ( i = 0; i < n; i++ )
                printf("%c", c);

        printf("\n");
}

int main()
{
        char line[100], other[100];
        int i, len;

        printf("Phrase: ");
        fgets( line, 100, stdin );

        // fix the string....
        // ...
//              really, do this
        // ...

        rept(10,'-');
        printf("Orig:  @@%s@@\n", line);

        rept(10,'-');
        makeUpper( line, other );
        printf("toupper: %s\n", other);

        rept(10,'-');
        i = countDigit( line );
        printf("%d digits\n", i);

        rept(10,'-');
        len = onlyLetters( line, other );
        printf("only letters: %s\n", other );
        printf(" new len %d\n", len);

        int nums[30], vals[30];
        int many = 19;

        rept(5, '-');
        fillNumList( nums, many, 3, 11 );
        printList( nums, many );

        rept(5, '-');
        i = countOdd( nums, many );
        printf("%d odd values\n", i);
        rept(5, '-');
        i = sumList( nums, many );
        printf("%d is sum\n", i);

        rept(5, '-');
        copyNumList( nums, many, vals );
        printf("Copy\n");
        printList( vals, many );

        rept(5, '-');
        addToEach( vals, many, 4 );
        printf("Add 4\n");
        printList( vals, many );

        rept(5, '-');
        printf("Orig\n");
        printList( nums, many );
        rept(5, '-');

        return 0;
}

标签: carraysstringfunctionint

解决方案


要回答一个几乎定义明确的问题(关于makeUpper):

首先,str不是变量。你有origand result,并且没有str在任何地方声明。

现在,如果strwere orig,您将正确地大写原始字符串中的字符。但是,任务是保留原始字符串并制作大写副本。前者称为“破坏性操作”(不保留原始内容);后者,一种“非破坏性操作”(出于显而易见的原因)。以下是使您的函数具有非破坏性所需的更改:

void makeUpper( char orig[], char result[] )
{
        int k = 0;

        do
        {
                if ( orig[k] >= 'a' && orig[k] <= 'z' )
                        result[k] = orig[k] - 32;
                else
                        result[k] = orig[k];

        } while ( orig[k++] != '\0' );
}

请注意,我在 上迭代和测试orig,但分配给result.

onlyLetters应该是相似的;但是您将k需要两个索引变量,orig而不仅仅是复制 - 而不是,您将始终在每个循环中消耗一个字符)。resultresultorig

由于您没有说明您的误解是什么copyNumListfillNumList因此我无法为您提供任何帮助。

编辑:我忘记了终结者(并且没有测试它);谢谢汤姆卡泽斯!因为零也需要被复制,所以你的循环从更改while (condition) { ... }do { ... } while (condition)- 而不是测试我们是否结束,然后如果我们不是,我们会先做这件事,然后才询问我们是否'重做。这也需要在测试之后进行增量,所以k++被移动了。如果这对您来说太奇怪了,您可以通过在循环完成后显式添加终止符来获得类似的效果:

void makeUpper( char orig[], char result[] )
{
        int k = 0;

        while ( orig[k] != '\0' )
        {
                if ( orig[k] >= 'a' && orig[k] <= 'z' )
                        result[k] = orig[k] - 32;
                else
                        result[k] = orig[k];

                k++;
        }
        result[k] = '\0';
}

推荐阅读