【问题标题】:Array of char in C + accessing each charC +中的char数组访问每个char
【发布时间】:2019-07-30 11:12:57
【问题描述】:

我需要输入 n 行,每行带有 1 个“字符串”(char 数组)。然后我想将它们存储在一个 Word 结构中并打印每个。我需要访问每个“字符串”的第一个和最后一个字符。

这些字符串在每个字符后总是有一个 q 字母(最后一个除外)。 示例:你好 -> Hqeqlqlqo

最重要的事情,我还需要调用一个函数来接收“字符串”,然后取消“q”并将每个字符添加到这个新的结构 WordModified,这样我就可以将它们全部打印在结束

所以,我的问题是:如何按字符访问两个结构的单词?以及如何构建我的函数以排除“q”并连续附加 WordModified 结构中单词的字母。

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAXCHAR 1100


typedef struct {
  char name[1101];
} WordReceived;

typedef struct {
  char name[1101];
} WordModified;


int main(void)
{
  //char input[1101];
  int nrlines, i;

  scanf("Number of lines %d\n", &nrlines);
  WordReceived words[nrlines];
  WordModified wordsMod[nrlines];

  for (i = 0; i < nrlines; ++i)
  {
    printf("String\n");
    scanf("%s\n", words[i].name);
  }

  for (i = 0; i < nrlines; ++i)
  {
    printf("word %d: %s\n", i+1, words[i].name);
    printf("First char: %s\n",  words[i].name[0]);
    printf("Last char: %s\n",  words[i].name[n-1]);
  }

  for (i = 0; i < nrlines; ++i)
  {
    printf("word %d: %s\n", i+1, wordsMod[i].name);
  }

  return 0;
}

【问题讨论】:

  • 那么你的问题到底是什么?
  • 我刚刚编辑了,到最后了
  • 为什么需要两个相同类型但类型名称不同的类型?为什么struct 和一个char[] 成员必须是struct?看起来你让事情变得比他们需要的更难。回答“我如何逐个字符地访问两个结构的单词?”:通过编写采用char * 类型的函数。
  • void copyodd(char *dst, const char *src) { while(*src) { *dst = *src; dst++; src++; if (*src) src++; } *dst = 0; }
  • 如果您需要打印一个没有字符的字符串。打印从零到小于长度的字符串,步长为 2。

标签: c arrays data-structures struct char


【解决方案1】:

带有'q'字符的函数很简单。请记住,我没有处理包含'q'字符的情况,你可以练习。

我修改了一点你的代码,你可以看到我的 cmets。

我不明白你为什么要访问每个单词的第一个和最后一个字符。

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#define MAXCHAR 1100 //What is for?

void CleanWord(char *word, char* mod_word);

typedef struct {
  char name[1101];
} WordReceived;

typedef struct {
  char name[1101];
} WordModified;


int main(void)
{
  //char input[1101];
  int nrlines, i;

  printf("Number of lines: \n");
  scanf(" %d", &nrlines);
  WordReceived words[nrlines];
  WordModified wordsMod[nrlines];

  memset(words, 0, sizeof(words));      //Initialize the struct
  memset(words, 0, sizeof(wordsMod));   //Initialize the struct

  for (i = 0; i < nrlines; ++i)
  {
    printf("String\n");
    scanf(" %s", words[i].name);
  }


  for (i = 0; i < nrlines; ++i)
  {
    CleanWord(words[i].name, wordsMod[i].name);

    printf("word %d: %s\n", i+1, words[i].name);
    printf("First char: %c\n",  words[i].name[0]);      //your code has %s formating but the variable is signle character

    int n = strlen(words[i].name);  //store the length of string
    printf("Last char: %c\n",  words[i].name[n-1]);
  }

   for (i = 0; i < nrlines; ++i)
   {
    printf("word %d: %s\n", i+1, wordsMod[i].name);
   }

  return 0;
}

/*  This function remove the char 'q' from the 'word' and store the result to 'mod_word'*/
void CleanWord(char* word, char* mod_word)
{
    int i,j = 0;
    int word_size = strlen(word);

    for(i = 0; i < word_size; i++)
    {
        if(word[i] != 'q')
            mod_word[j++] = word[i];
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-19
    • 2011-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-30
    • 1970-01-01
    • 2018-05-01
    相关资源
    最近更新 更多