简体   繁体   English

C程序-将字符串数组合并到程序中时出错

[英]C Program - Error Combine String Array into Program

I'm trying to write program to ask user to enter First and Last Name. 我正在尝试编写程序以要求用户输入名字和姓氏。 Then my program will result their Full Name (combined First + Last Name) and the length of their Full Name. 然后,我的程序将得出其全名(姓和名的组合)以及其全名的长度。 My Output right now does empty Full Name and 0 length. 我的输出现在没有空的全名和0长度。 I guess my problem is at display_name functions. 我想我的问题出在display_name函数上。 Here is my code so far. 到目前为止,这是我的代码。 Thank you. 谢谢。

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

void display_name(char *fullname);

int count_char( char*x_ptr);    

char * get_name(char * first_name, char * last_name);

#define MAX 80     // maximum number of array elements 

int main(void)
{
    char first_name[MAX];
    char last_name[MAX];
    char *x_ptr; 

    system("cls");
    printf("Enter Last Name: \n" );
    scanf("%s", &last_name );
    printf("Enter First Name: \n" );
    scanf("%s", &first_name );

    x_ptr = get_name(first_name, last_name);

    display_name(x_ptr);

    puts("");                                    
    system("pause");                              
    return 0;                                                             
}

char * get_name(char *first_name, char *last_name)
{
    static char fullname[MAX];
    char x;
    x = 0;
    strcpy(fullname, first_name);
    strcat(fullname, " ");
    strcat(fullname, last_name);


    while (((fullname[x] = getchar()) != '\n') && (x < (MAX-1))) 
    {
            x++;                                          
    }
    fullname[x] = '\0';     
    return(fullname); 

}
/* Function to print out string passed to it and display the length of fullname*/
void display_name(char *fullname)
{
    char *a;                                    
    printf("Your Full name is ");
    a = &fullname[0];                              
    while (*a != '\0')                              
      {
            putchar(*a);                              
            a++;                                    
      }

    int length;
    length  =  strlen(fullname); 
    printf("\nHas %d Characters",  length);  
    length = count_char(fullname);
    printf("\nHas %d Non Space Characters", length); 

}
/* function to return count of non space characters*/
int count_char( char * x_ptr)
{
        char *b;                                    
    unsigned int count=0;                        
    b = x_ptr;                                    
    while (*b != '\0')                              
    {
            if (*b != ' ')                        
                  count++;                        
            b++;                                    
    }
return
(count);                              
}
scanf("%s", &last_name );

Compiler complained and you ignored it. 编译器抱怨,您忽略了它。 It should be scanf("%s", last_name ); 应该是scanf("%s", last_name ); . Same goes with firstname . firstname Yours had type char (*)[] and scanf expects char* which is what we gave in second case. 您的类型为char (*)[] ,scanf期望使用char* ,这是我们在第二种情况下给出的。

This part is doing nothing that you would do to achieve what you are trying to do. 这部分没有做您将要实现的目标。

while (((fullname[x] = getchar()) != '\n') && (x < (MAX-1))) 

This is using getchar to get the characters from stdin and put it in the char array where you are storing the concatenated name. 这是使用getcharstdin获取字符,并将其放入存储串联名称的char数组中。

Using static char array is not a good solution. 使用static char数组不是一个好的解决方案。 The next time you try to use this function - it will overwrite the data previously written by another function. 下次尝试使用此功能时-它会覆盖先前由另一个功能写入的数据。 Illustration implementation of the function get_name would be 函数get_name示例实现为

char * get_name(char *first_name, char *last_name)
{
    char *fullname = malloc(strlen(first_name)+2+strlen(last_name));
    if(!fullname){
       perror("malloc");
       exit(EXIT_FAILURE);
    }
    strcpy(fullname, first_name);
    strcat(fullname, " ");
    strcat(fullname, last_name);
    return fullname;
}

Benefit of using this implementation is that - now the data that is being used is not closely coupled with the methods that call this one. 使用此实现的好处是-现在正在使用的数据与调用此方法的方法并不紧密耦合。 So it can be reused independent of it's previous usage in another function. 因此,它可以独立于其他功能中的先前用法进行重用。

Also when using the function get_name remember to free the dynamically allocated memory when you are done working with it. 同样,在使用函数get_name请记住在使用完动态分配的内存get_namefree

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM