Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 如何将反向字符串函数从主函数转换为自己的独立函数?_C - Fatal编程技术网

C 如何将反向字符串函数从主函数转换为自己的独立函数?

C 如何将反向字符串函数从主函数转换为自己的独立函数?,c,C,我正在努力学习C语言并理解它 请参见下面的示例代码,您将如何创建一个名为“reverseString”的函数,该函数将“for”循环从“main”函数中去掉 #include <stdio.h> #include <string.h> #define NUMBER_OF_STRING 100 #define MAX_STRING_SIZE 50 void print_array(const char commonWords[NUMBER_OF_STRING][MA

我正在努力学习C语言并理解它

请参见下面的示例代码,您将如何创建一个名为“reverseString”的函数,该函数将“for”循环从“main”函数中去掉

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

#define NUMBER_OF_STRING 100
#define MAX_STRING_SIZE   50

void print_array(const char commonWords[NUMBER_OF_STRING][MAX_STRING_SIZE])
{
    int count = 1;
    for (int i = 0; i < NUMBER_OF_STRING; i++, count++) {
        printf("%_7s is word number: %d\n", commonWords[i], count);
    }
}


int main() {
    char commonWords[NUMBER_OF_STRING][MAX_STRING_SIZE] = {"the", "be", "to", "of", "and", "a", "in", "that", "have", "I", "it", "for", "not", "on", "with", "he", "as", "you", "do", "at", "this", "but", "his", "by", "from", "they", "we", "say", "her", "she", "or", "an", "will", "my", "one", "all", "would", "there", "their", "what", "so", "up", "out", "if", "about", "who", "get", "which", "go", "me", "when", "make", "can", "like", "time", "no", "just", "him", "know", "take", "people", "into", "year", "your", "good", "some", "could", "them", "see", "other", "than", "then", "now", "look", "only", "come", "its", "over", "think", "also", "back", "after", "use", "two", "how", "our", "work", "first", "well", "way", "even", "new", "want", "because", "any", "these", "give", "day", "most", "us"};

    printf("Before reverse:\n");
    print_array(commonWords);

    // *** reverse function which requires its own function ***
    for (int i = 0; i < NUMBER_OF_STRING; i++)
    {
        for (int j = 0, k = strlen(commonWords[i]) - 1; j < k; j++, k--)
        {
            char temp = commonWords[i][j];
            commonWords[i][j] = commonWords[i][k];
            commonWords[i][k] = temp;
        }
    }

    printf("\nAfter reverse:\n");
    print_array(commonWords);


    return 0;
}
参考文献:

Before reverse:
    the is word number: 1
     be is word number: 2
     to is word number: 3
     of is word number: 4
    and is word number: 5
      a is word number: 6
     in is word number: 7
   that is word number: 8
   have is word number: 9
      I is word number: 10
     it is word number: 11
    for is word number: 12
    not is word number: 13
     on is word number: 14
   with is word number: 15
     he is word number: 16
     as is word number: 17
    you is word number: 18
     do is word number: 19
     at is word number: 20
   this is word number: 21
    but is word number: 22
    his is word number: 23
     by is word number: 24
   from is word number: 25
   they is word number: 26
     we is word number: 27
    say is word number: 28
    her is word number: 29
    she is word number: 30
     or is word number: 31
     an is word number: 32
   will is word number: 33
     my is word number: 34
    one is word number: 35
    all is word number: 36
  would is word number: 37
  there is word number: 38
  their is word number: 39
   what is word number: 40
     so is word number: 41
     up is word number: 42
    out is word number: 43
     if is word number: 44
  about is word number: 45
    who is word number: 46
    get is word number: 47
  which is word number: 48
     go is word number: 49
     me is word number: 50
   when is word number: 51
   make is word number: 52
    can is word number: 53
   like is word number: 54
   time is word number: 55
     no is word number: 56
   just is word number: 57
    him is word number: 58
   know is word number: 59
   take is word number: 60
 people is word number: 61
   into is word number: 62
   year is word number: 63
   your is word number: 64
   good is word number: 65
   some is word number: 66
  could is word number: 67
   them is word number: 68
    see is word number: 69
  other is word number: 70
   than is word number: 71
   then is word number: 72
    now is word number: 73
   look is word number: 74
   only is word number: 75
   come is word number: 76
    its is word number: 77
   over is word number: 78
  think is word number: 79
   also is word number: 80
   back is word number: 81
  after is word number: 82
    use is word number: 83
    two is word number: 84
    how is word number: 85
    our is word number: 86
   work is word number: 87
  first is word number: 88
   well is word number: 89
    way is word number: 90
   even is word number: 91
    new is word number: 92
   want is word number: 93
because is word number: 94
    any is word number: 95
  these is word number: 96
   give is word number: 97
    day is word number: 98
   most is word number: 99
     us is word number: 100

After reverse:
    eht is word number: 1
     eb is word number: 2
     ot is word number: 3
     fo is word number: 4
    dna is word number: 5
      a is word number: 6
     ni is word number: 7
   taht is word number: 8
   evah is word number: 9
      I is word number: 10
     ti is word number: 11
    rof is word number: 12
    ton is word number: 13
     no is word number: 14
   htiw is word number: 15
     eh is word number: 16
     sa is word number: 17
    uoy is word number: 18
     od is word number: 19
     ta is word number: 20
   siht is word number: 21
    tub is word number: 22
    sih is word number: 23
     yb is word number: 24
   morf is word number: 25
   yeht is word number: 26
     ew is word number: 27
    yas is word number: 28
    reh is word number: 29
    ehs is word number: 30
     ro is word number: 31
     na is word number: 32
   lliw is word number: 33
     ym is word number: 34
    eno is word number: 35
    lla is word number: 36
  dluow is word number: 37
  ereht is word number: 38
  rieht is word number: 39
   tahw is word number: 40
     os is word number: 41
     pu is word number: 42
    tuo is word number: 43
     fi is word number: 44
  tuoba is word number: 45
    ohw is word number: 46
    teg is word number: 47
  hcihw is word number: 48
     og is word number: 49
     em is word number: 50
   nehw is word number: 51
   ekam is word number: 52
    nac is word number: 53
   ekil is word number: 54
   emit is word number: 55
     on is word number: 56
   tsuj is word number: 57
    mih is word number: 58
   wonk is word number: 59
   ekat is word number: 60
 elpoep is word number: 61
   otni is word number: 62
   raey is word number: 63
   ruoy is word number: 64
   doog is word number: 65
   emos is word number: 66
  dluoc is word number: 67
   meht is word number: 68
    ees is word number: 69
  rehto is word number: 70
   naht is word number: 71
   neht is word number: 72
    won is word number: 73
   kool is word number: 74
   ylno is word number: 75
   emoc is word number: 76
    sti is word number: 77
   revo is word number: 78
  kniht is word number: 79
   osla is word number: 80
   kcab is word number: 81
  retfa is word number: 82
    esu is word number: 83
    owt is word number: 84
    woh is word number: 85
    ruo is word number: 86
   krow is word number: 87
  tsrif is word number: 88
   llew is word number: 89
    yaw is word number: 90
   neve is word number: 91
    wen is word number: 92
   tnaw is word number: 93
esuaceb is word number: 94
    yna is word number: 95
  eseht is word number: 96
   evig is word number: 97
    yad is word number: 98
   tsom is word number: 99
     su is word number: 100

Process finished with exit code 0

您已经有了一个带有
print\u array
的函数示例。使用它编写函数
reverseString
@SergeBallesta我知道我已经尝试过了,但是遇到了错误…然后显示您尝试过的内容。我们将能够告诉您它抛出错误的原因以及如何修复。无论如何,如果你仔细阅读,你知道你应该表现出诚实的尝试……这句话有没有警告:
char temp=string[j]?而
strlen(string)
语句应该在循环之前求值,而不是在循环中求值:
int len=strlen(string)
,然后在循环语句中使用
len
char *reverseString(char *string)
{
        for (int j = 0, k = strlen(string) - 1; j < k; j++, k--)
        {
            char temp = string[j];
            string[j] = string[k];
            string[k] = temp;
        }
        return string;
}

    // *** reverse function which requires its own function ***
    for (int i = 0; i < NUMBER_OF_STRING; i++)
    {
        reverseString(commonWords[i]);
    }