English 中文(简体)
C中不同论点的数目
原标题:variable number of arguments in C
  • 时间:2024-03-10 21:29:35
  •  标签:
  • c

我提出一个问题,争论不一。 C. Im 脂重 在我的任务中,需要具体说明未申报参数中的类型。 这似乎没有任何理由这样做,因为一米仅使用护卫,但需要使用“带”;stdarg.h>。 该职能可以打印文字,具体文字重复三倍。 因此,当我尝试这一法典时:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

void repeatingLetter(char* sentence, char letter) 
{
    char* word = strtok(sentence, " ");

    while (word != NULL) {
        int count = 0;
        int len = strlen(word);

        for (int i = 0; i < len; i++) {
            if (word[i] == letter) {
                count++;
            }
        }

        if (count >= 3) {
            printf("%s
", word);
        }

        word = strtok(NULL, " ");
    }
}

void findWords(int numSentences, ...) 
{
    va_list args;
    va_start(args, numSentences);

    char letter;
    printf("Enter a letter to check: ");
    scanf("%c", &letter);

    for (int i = 0; i < numSentences; i++)
    {
        char* sentence = va_arg(args, char*);

        printf("Words in  %s  with  %c  repeated 3 or more times:
", sentence, letter);
        repeatingLetter(sentence, letter);
        printf("
");
    }

    va_end(args);
}

int main() 
{
    int numSentences;
    printf("Enter a number of sentences: ");
    scanf("%d", &numSentences);
    getchar(); 

    char** sentences = (char**)malloc(numSentences * sizeof(char*));

    for (int i = 0; i < numSentences; i++)
    {
        sentences[i] = (char*)malloc(100 * sizeof(char));
        printf("Enter a sentences %d: ", i + 1);
        fgets(sentences[i], 100, stdin);
        sentences[i][strcspn(sentences[i], "
")] =   ;
    }

    findWords(numSentences, sentences);

    for (int i = 0; i < numSentences; i++) 
    {
        free(sentences[i]);
    }
    free(sentences);

    return 0;
}

I:

    Enter a number of sentences: 3
    Enter a sentences 1: Hello world
    Enter a sentences 2: oooooops
    Enter a sentences 3: gdsgdgdg
    Enter a letter to check: o
    Words in  ╨Ъ.┌c  with  o  repeated 3 or more times:
    
    Words in  ╨Ъ.┌c  with  o  repeated 3 or more times:
    
    Words in  o
    
    gdgdg
    
    d
    
      with  o  repeated 3 or more times:

我不理解问题是什么。 您能否帮助我?

我曾试图以愤怒和浮动的方式这样做。 不存在任何问题,但我无法理解在扼杀方面有哪些错误。

问题回答

这里有not几个不同的论点。 有两个方面:你为这一职能提供一张记号,并通过这个记号,你可以查阅你为保存数据而分配的一套记忆。

为了使其目前从<代码>main中援引,您的<代码>限值/代码>的最低固定功能是:

void findWords(int numSentences, char** sentences) 
{
    char letter;
    printf("Enter a letter to check: ");
    scanf("%c", &letter);

    for (int i = 0; i < numSentences; i++)
    {
        char* sentence = sentences[i];

        printf("Words in  %s  with  %c  repeated 3 or more times:
", sentence, letter);
        repeatingLetter(sentence, letter);
        printf("
");
    }
}

不同论点的全点是,如果你有可以要求的职能,那么,就好了。 详情请参见fscanf。 这两者都可以得到一项或多项论点,而你应当提供的实际论点数目由第一个论点确定。

你们不需要。 你把“可变的论据”与“可变的要素数目”混为一谈。 你们都需要做的是告诉你有多少刑期,以及在哪里找到这些刑期。

在我的任务中,需要具体说明未申报参数中的类型。 这似乎没有任何理由这样做,因为一米仅使用护卫,但需要使用“带”;stdarg.h>。 该职能可打印文字,具体文字重复3+倍

这些参数不是未申报的><>>>>。 这就是<代码>stdargs。

还有一个功能是,将具备这些参数,如你所说,所有缩略语,并打印所有字母至少有<代码>n倍。

贵方案的可能目标是将这一职能从variadic=上调,并提出不同的论据。 如果不是目标,请说明任务要求。

it is easier to solve one problem each time

Complete code is at the end for this example

1 of 2: the function

void repeatingLetter(char* sentence, char letter)
{
    char* word = strtok(sentence, " ");

    while (word != NULL)
    {
        int count = 0;
        int len   = strlen(word);

        for (int i = 0; i < len; i++)
        {
            if (word[i] == letter) { count++; }
        }

        if (count >= 3) { printf("%s
", word); }

        word = strtok(NULL, " ");
    }
}

About the original code

  • do not printf() things inside the target function.
  • do not return void
  • the function scans a word, not a sentence
  • do not write interactive code. It only slows down everything

<代码>C<0>>。 只有<代码>0>。 指示数与标准不符。 仅此。 如果你有句子,将句子打成言语,然后用字标出功能......

Example

int repeatingLetter(
  const char* word, const char letter, size_t times)
{
  if (word == NULL) return 0;
  if ((letter == 0) && (times == 0)) return 0;
  char*  pl = (char*)word;  // pointer to 1st letter
  size_t count = 0;
  while (*pl != 0)
  {
      if (*pl == letter)
          if (++count == times) return 1;
      ++pl;
  }
  return 0;
}

这不过是这样,但只不过是这样。 返回代码<1> [> 字母> 至少出现在<代码>中倍于< 密码>。

测试你只能提前书写另一个功能,如此。

int t_repeat(const char* s, const char letter, size_t times)
{
    printf("

    sentence: "%s"

    letter:  %c 

    total number of times: %llu
",
        s, letter, times);
    int res = repeatingLetter(s, letter, times);
    printf("
	function returned %d
", res);
    return 0;
}

这是微不足道的,但如果你以前这样做,就可以节省一些时间。 见main():

int main(void)
{
    t_repeat("aaa",  a , 3);
    t_repeat("aa",  a , 3);
    t_repeat("aaa",  a , 4);
    t_repeat("aaa", 0, 4);
    t_repeat("aaa",  a , 0);
    t_repeat(NULL,  a , 0);
    return 0;
}

因此,在考虑方案的其他内容之前,很容易确定某些条件并对其进行测试。

http://www.ohchr.org。


    sentence: "aaa"
    letter:  a 
    total number of times: 3

        function returned 1

    sentence: "aa"
    letter:  a 
    total number of times: 3

        function returned 0

    sentence: "aaa"
    letter:  a 
    total number of times: 4

        function returned 0

    sentence: "aaa"
    letter:   
    total number of times: 4

        function returned 0

    sentence: "aaa"
    letter:  a 
    total number of times: 0

        function returned 0

    sentence: "(null)"
    letter:  a 
    total number of times: 0

        function returned 0

因此,似乎正在ok着。

2 of 2: calling the function with a variable number of strings

findWords 是

void findWords(
    const char letter,
    const size_t n_times,
    const size_t N, ...)

然后main()用于测试的简单明了。

int main(void)
{
    findWords( x , 3, 8,
        "xvalue",
        "xvaluexx",
        "xxxAll",
        "Sxtxaxck",
        "Sxtxaxck",
        "Stack Overflow",
        "xOverflowx",
        "xOverflowxx"
    );
    return 0;
};  // main()

http://www.ohchr.org。

"xvaluexx"
"xxxAll"
"Sxtxaxck"
"Sxtxaxck"
"xOverflowxx"

此处为<代码>定型Words的简单实施:

// find  l  at least  t  times in each of the 
//  N  arguments in  ... 
void findWords(
    const char l, const size_t t, const size_t N, ...)
{
    va_list args;
    va_start(args, N);
    for (size_t i = 0; i < N; i++)
    {
        char* string = va_arg(args, char*);
        if (repeatingLetter(string, l, t) != 0)
            printf(""%s"
",string);
    }
    va_end(args);
}

在第一次测试中,与<代码>t_repeat(几乎相同。

complete code for the example

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

int  repeatingLetter(const char*, const char, size_t);
void findWords(const char, const size_t, const size_t, ...);

int main(void)
{
    findWords( x , 3, 8,
        "xvalue",
        "xvaluexx",
        "xxxAll",
        "Sxtxaxck",
        "Sxtxaxck",
        "Stack Overflow",
        "xOverflowx",
        "xOverflowxx"
    );
    return 0;
};  // main()

int repeatingLetter(
    const char* word, const char letter, size_t times)
{
    if (word == NULL) return 0;
    if ((letter == 0) && (times == 0)) return 0;
    char*  pl    = (char*)word;  // pointer to 1st letter
    size_t count = 0;
    while (*pl != 0)
    {
        if (*pl == letter)
            if (++count == times) return 1;
        ++pl;
    }
    return 0;
}

// find  l  at least  t  times in each of the 
//  N  arguments in  ... 
void findWords(
    const char l, const size_t t, const size_t N, ...)
{
    va_list args;
    va_start(args, N);
    for (size_t i = 0; i < N; i++)
    {
        char* string = va_arg(args, char*);
        if (repeatingLetter(string, l, t) != 0)
            printf(""%s"
",string);
    }
    va_end(args);
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...