C:通过struct和concat循环到字符串

问题描述 投票:0回答:3

我有具有以下结构的C程序:

struct {      
    char *ext;
    char *filetype;
} extensions [] = {
    {"gif", "image/gif" },
    {"jpg", "image/jpg" },
    {"jpeg","image/jpeg"},
    {"png", "image/png" },
    {0,0}     
};            

如何创建返回仅包含以新行分隔的扩展名的字符串的函数?基本上,这是能够执行此操作的条件:

printf("\nThe following extensions are supported:\n%s",GetExtensions());

并输出此内容:

The following extensions are supported:
.gif
.jpg
.jpeg
.png

我认为循环部分正确,但我不理解如何将每个ext + \n并置为一个字符串:

#include <leaving these off for brevity...>

struct {
    char *ext;
    char *filetype;
} extensions [] = {
    {"gif", "image/gif" },
    {"jpg", "image/jpg" },
    {"jpeg","image/jpeg"},
    {"png", "image/png" },
    {0,0}     
};

char *getExtensions(void) {
    char* exts;
    int i;
    for(i=0;extensions[i].ext != 0;i++){

        // What do I do here?? 

    }
    return exts;
}

int main(int argc, char **argv){
    printf("\nThe following extensions are supported: \n%s",GetExtensions());
}
c for-loop concatenation c-strings strcat
3个回答
0
投票
#include <stdio.h>
#include <string.h>

// Your struct
struct extensionInfo {
   char *ext;
   char *filetype;
};

struct extensionInfo extensions [] = {
   {"gif", "image/gif" },
   {"jpg", "image/jpg" },
   {"jpeg","image/jpeg"},
   {"png", "image/png" },
   {0,0}
};

int main(int argc, char **args, char **env) {
   char buffer[1024];
   struct extensionInfo *ext;

   // Initialize the buffer
   memset(buffer, 0, sizeof(buffer));

   // Insert your first text.
   strncat(buffer, "The following extensions are supported:", sizeof(buffer) - 1);

   // Loop through your array and append everything
   for (ext = extensions; ext->ext != 0; ext++) {
      strncat(buffer, "\n", sizeof(buffer) - 1);
      strncat(buffer, ext->ext, sizeof(buffer) - 1);
   }

   // Show your result
   printf("%s\n", buffer);

   return 0;
}

这里是一个有效的注释示例。如有任何疑问,请随时提问。


0
投票

我认为以下是您想要的。您可以阅读评论以了解其工作原理。

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

struct {
    char *ext;
    char *filetype;
} extensions [] = {
        {"gif", "image/gif" },
        {"jpg", "image/jpg" },
        {"jpeg","image/jpeg"},
        {"png", "image/png" },
        {0,0}
};

char *getExtensions(void) {
    size_t count = 0;
    // let's know how many characters are needed for all extensions
    for(int i = 0; extensions[i].ext != 0; ++i) {
        size_t j = 0;
        for (; extensions[i].ext[j] != 0; ++j) ;
        count += j + 1; // +1 for every newline
    }

    // +1 for null terminator
    char *str = calloc(count + 1, sizeof(char));

    // let's concatenate
    for(size_t i = 0; extensions[i].ext != 0; ++i) {
        strcat(str, extensions[i].ext);
        strcat(str, "\n");
    }

    return str;
}

int main(int argc, char **argv){
    char * const result = getExtensions();
    printf("\nThe following extensions are supported: \n%s", result);
    free(result);
}

0
投票

看来您的意思是以下内容。

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

struct extension
{
    char *ext;
    char *filetype;
};

char * getExtensions( const struct extension a[] )
{
    size_t n = 0;

    for ( size_t i = 0; a[i].ext != NULL; i++ )
    {
        n += strlen( a[i].ext ) + 1;
    }

    char *s = malloc( n + 1 );

    if ( s != NULL )
    {
        s[0] = '\0';

        for ( size_t i = 0; a[i].ext != NULL; i++ )
        {
            strcat( s, a[i].ext );
            strcat( s, "\n" );
        }
    }

    return s;
}

int main(void) 
{
    struct extension extensions [] = 
    {
        {"gif", "image/gif" },
        {"jpg", "image/jpg" },
        {"jpeg","image/jpeg"},
        {"png", "image/png" },
        { NULL, NULL }     
    };

    char *s = getExtensions( extensions ); 

    printf( "The following extensions are supported:\n%s", s );

    free( s );

    return 0;
}

程序输出为

The following extensions are supported:
gif
jpg
jpeg
png

0
投票

这是您要实现的目标的可行示例:

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

struct {
    char *ext;
    char *filetype;
} extensions [] = {
    {"gif", "image/gif" },
    {"jpg", "image/jpg" },
    {"jpeg","image/jpeg"},
    {"png", "image/png" },
    {0,0}     
};

char *getExtensions(void) {
    char* exts = NULL;
    int i;
    int totalSize = 0;
    for(i=0; extensions[i].ext != 0;i++){
        totalSize += strlen(extensions[i].ext)+1; 
    }

    exts = (char *)calloc(totalSize + 1, sizeof(char));

    int index = 0;
    for(i=0; extensions[i].ext != 0; i++){
    strcat(exts, extensions[i].ext);
    strcat(exts, "\n");
    index += strlen(extensions[i].ext) + 1;
    }



    return exts;
}

int main(int argc, char **argv){
    printf("\nThe following extensions are supported: \n%s",getExtensions());
}
© www.soinside.com 2019 - 2024. All rights reserved.