C:遍历结构并连接到字符串
C: Loop through struct and concat to string
我有 C
具有以下结构的程序:
struct {
char *ext;
char *filetype;
} extensions [] = {
{"gif", "image/gif" },
{"jpg", "image/jpg" },
{"jpeg","image/jpeg"},
{"png", "image/png" },
{0,0}
};
如何创建一个 returns 字符串的函数,该字符串仅包含由换行符分隔的扩展名?基本上这是能够做到这一点:
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());
}
#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;
}
这是一个有效的注释示例。如果您有任何问题,请随时提出。
我想下面是你想要的。您可以阅读评论以了解其工作原理。
#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);
}
看来你的意思是下面的。
#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] = '[=10=]';
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
我有 C
具有以下结构的程序:
struct {
char *ext;
char *filetype;
} extensions [] = {
{"gif", "image/gif" },
{"jpg", "image/jpg" },
{"jpeg","image/jpeg"},
{"png", "image/png" },
{0,0}
};
如何创建一个 returns 字符串的函数,该字符串仅包含由换行符分隔的扩展名?基本上这是能够做到这一点:
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());
}
#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;
}
这是一个有效的注释示例。如果您有任何问题,请随时提出。
我想下面是你想要的。您可以阅读评论以了解其工作原理。
#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);
}
看来你的意思是下面的。
#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] = '[=10=]';
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