在循环中收集字符串并打印出所有字符串外部循环

时间:2012-07-07 03:03:35

标签: c string pointers loops char

我是新手,有一些问题,我想从你们那里得到一些教训。 例如:

#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>

void main()
{
    char name[51],selection;

    do
    {

    printf("Enter name: ");
    fflush(stdin);
    fgets(name,51,stdin);
    printf("Enter another name?(Y/N)");
    scanf("%c",&selection);
    selection=toupper(selection);
    }while (selection=='Y');
                     //I want to printout the entered name here but dunno the coding
printf("END\n");
system("pause");
}

据我所知,循环执行时会覆盖变量,那么我如何执行打印输出用户输入的所有名称的编码? 我已经问过我的导师他要求我使用指针,在这种情况下有人可以指导我吗?

3 个答案:

答案 0 :(得分:1)

您基本上有两个选项,创建所有名称的列表,在最后循环遍历每个名​​称,或者在读取时将所有名称连接成字符串,并在结尾处打印整个缓冲区。第一个大致如下:

 char ch[2]="Y", names[100][52]; //store up to 100 50-char names (leaving space for \n and \0)
 int x, nnames=0;
 while(nnames<100 && *ch=='Y'){
     fgets(names[nnames++], sizeof names[0], stdin); //since names is a 2d array, look
     //only at the length of a row, (in this case, names[0])
     fgets(ch, 2, stdin);
     *ch = toupper(*ch);
 }
 for(x=0; x<nnames; x++)
     //print names[x].

第二个大致如下:

 char names[100*52], *np=names, *ep=names+sizeof names; //an array, the position, and the end
 char ch[2]="Y";
 while(np<ep && *ch=='Y'){
      fgets(np, ep-np, stdin); //read a name, and a newline into the buffer
      //note how I use the difference between the end and the position to tell
      //fgets the most it can possibly read
      np+=strlen(np); //advance the position to the end of what we read.
      //same deal with the y/n stuff...
 }
 printf("%s", names);

注意最后缺少循环,因为整个字符串都存储在名称中。

答案 1 :(得分:0)

您可以使用链接列表存储所有输入的名称。所有数据结构书籍都经常谈论链表。

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

struct name_node
{
    char name[100];
    struct name_node *next;
};

struct name_node* add_name(struct name_node *first, const char *name)
{
    struct name_node *p;

    /* create our node */
    p = (struct name_node*)malloc(sizeof(struct name_node));
    strcpy(p->name, name);
    p->next = NULL;

    /* link our node to the tail */
    if (first) {
        for (; first->next; first = first->next);
        first->next = p;
    }

    return p;
}

void print_names(struct name_node *first)
{
    /* print names stored in the list */
    for (; first; first = first->next) {
        printf("%s\n", first->name);
    }
}

/* free the memory we used */
void destroy_list(struct name_node *first)
{
    if (first) {
        if (first->next)
            destroy_list(first->next);

        free(first);
    }
}

int main(void)
{
    struct name_node *head = NULL;
    struct name_node *node;
    char name[100];
    char selection;

    do {
        printf("Enter name: ");
        fflush(stdin);
        fgets(name, 51, stdin);

        /* save the user input to a linked list */
        /* save head if we don't have head */
        node = add_name(head, name);
        if (!head)
            head = node;

        printf("Enter another name?(Y/N)");
        scanf("%c", &selection);
        selection = toupper(selection);
    }
    while (selection == 'Y');

    /* print the list if we have any data */
    if (head)
        print_names(head);

    /* free the memory we used */
    destroy_list(head);

    printf("END\n");
    system("pause");

    return 0;
}

答案 2 :(得分:0)

使用单个字符串获取所有名称:

char allnames[1000] = "";

do {
    //get name in fgets
    selection = toupper(selection);
    strcat(allnames, selection);

    //Check for repetion

} while (/*there is repetion*/);

printf("%s", allnames);

您必须适当选择所有名称的大小以存储所有名称。