下の例の通りの関数を作りたいと思っています。return文の箇所の間違いがわからないので、教えてください。
例
accum("abcd") -> "A-Bb-Ccc-Dddd"
accum("RqaEzty") -> "R-Qq-Aaa-Eeee-Zzzzz-Tttttt-Yyyyyyy"
accum("cwAt") -> "C-Ww-Aaa-Tttt"
c
1 2#include <ctype.h> 3#include <string.h> 4#include <stdio.h> 5 6char *accum(const char *source) 7{ 8 int n; 9 char str1[255]; 10 char a = '-'; 11 12 for(n = 0; n < (int)strlen(source); n++) 13 { 14 15 sprintf(str1,"%c",toupper(source[n])); 16 17 if(n > 0) 18 { 19 for(int m =0; m < n; m++) 20 { 21 sprintf(str1,"%c",tolower(source[n])); 22 } 23 } 24 25 sprintf(str1,"%c",a); 26 } 27 28 return str1; 29}
エラーコード
solution.c:9:38: warning: 'sizeof source' will return the size of the pointer, not the array itself [-Wsizeof-pointer-div] for(n = 0; n < (int)(sizeof source / sizeof source[0]); n++) ~~~~~~~~~~~~~ ^ solution.c:12:5: warning: implicitly declaring library function 'sprintf' with type 'int (char *, const char *, ...)' [-Wimplicit-function-declaration] sprintf(str1,"%c",toupper(source[n])); ^ solution.c:12:5: note: include the header <stdio.h> or explicitly provide a declaration for 'sprintf' solution.c:25:11: warning: address of stack memory associated with local variable 'str1' returned [-Wreturn-stack-address] return str1; ^~~~ 3 warnings generated. 2 warnings and 6 errors generated.
修正版
c
1#include <ctype.h> 2#include <string.h> 3#include <stdio.h> 4 5char *accum(const char *source) 6{ 7 int n; 8 char str1[255]; 9 char a = '-'; 10 11 for(n = 0; n < (int)strlen(source); n++) 12 { 13 14 sprintf(str1,"%c",toupper(source[n])); 15 16 if(n > 0) 17 { 18 for(int m = 0; m < n; m++) 19 { 20 sprintf(str1,"%c",tolower(source[n])); 21 } 22 } 23 24 sprintf(str1,"%c",a); 25 } 26 27 return str1; 28}
修正版のエラー
solution.c:27:11: warning: address of stack memory associated with local variable 'str1' returned [-Wreturn-stack-address] return str1; ^~~~ 1 warning generated.
回答2件
あなたの回答
tips
プレビュー