質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

Q&A

解決済

2回答

917閲覧

5.3.コンパイルエラーが出てしまいます。

cuttman_bro

総合スコア7

C

C言語は、1972年にAT&Tベル研究所の、デニス・リッチーが主体となって作成したプログラミング言語です。 B言語の後継言語として開発されたことからC言語と命名。そのため、表記法などはB言語やALGOLに近いとされています。 Cの拡張版であるC++言語とともに、現在世界中でもっとも普及されているプログラミング言語です。

0グッド

0クリップ

投稿2020/10/21 08:36

キーボードから入力されたいくつかの単語を、まとめて一つの文字列にするプログラム を作成したいです。しかし、コンパイルエラーが出てしまいます。
プログラムの内容。

  1. 入力する単語の数は、マクロ NUM で4と定めておく。
  2. 入力する単語の長さは、(ヌル文字含め)20文字以下とする。入力用には、2次元の文字配列を用いる。
  3. 新しい文字列のための変数は newstr という文字型1次元配列とする。
  4. 入力された単語をつないで新しい文字列 newstr を作るが、 そのとき単語と単語の間にスペース一つを入れる。
  5. newstr 文字列のいちばん最後にはピリオド一つを入れる(スペースは無し;実行例参照)。
  6. 作成した newstr を丸ごと printf で出力する。 この時のprintf内でスペースやピリオドの挿入を行ってはならない。 最後に newstr の文字数(スペース・ピリオドも含む)を表示して終了(実行例参照)。
  7. strcpy関数やstrlen関数を使ってよい。strcpy関数は、第2引数に指定された文字列を、第1引数に指定されたアドレス以降にコピーする関数である。また、strlen関数は文字列の長さ(ヌル文字含まず)を返す関数である。 使用するときは、最初に string.h をインクルードしておく。

source

1#include <stdio.h> 2#include .......... 3 4#define NUM 4 5 6int main() 7{ 8 char str2[NUM][20]; /* 入力用の文字配列 */ 9 char newstr[100]; /* 出力用の文字配列 */ 10 int i; 11 12 13 printf( "Input %d words: \n", NUM ); 14 15 for( i = 0; i < NUM; i++ ){ 16 printf("%s", str2[i]); /* 元の文字列の表示 */ 17 } 18 19 for(i= 0; i < NUM; i++){ 20 21 strcpy(newstr[i],str[i]); 22 if ('A' <= str2[i][0] && str2[i][0] <= 'Z') 23 newstr[i][0] = str2[i][0] + 32; 24 //else break; 25 printf("%s\n", newstr[i]); 26 27 } 28 29 //newstr[i] = *prefix + str[i]; 30 31 32 33 //for( i = 0; i < NUM; i++ ){ 34 //printf("%s\n", newstr[i]); /* 新しく作成した文字列の表示 */ 35 //} 36 37 return 0; 38}

Error

1prog03.c:2:10: error: #include expects "FILENAME" or <FILENAME> 2 2 | #include .......... 3 | ^~~ 4prog03.c: In function ‘main’: 5prog03.c:21:5: warning: implicit declaration of function ‘strcpy’ [-Wimplicit-function-declaration] 6 21 | strcpy(newstr[i],str[i]); 7 | ^~~~~~ 8prog03.c:21:5: warning: incompatible implicit declaration of built-in function ‘strcpy’ 9prog03.c:2:1: note: include ‘<string.h>’ or provide a declaration of ‘strcpy’ 10 1 | #include <stdio.h> 11 +++ |+#include <string.h> 12 2 | #include .......... 13prog03.c:21:22: error: ‘str’ undeclared (first use in this function); did you mean ‘str2’? 14 21 | strcpy(newstr[i],str[i]); 15 | ^~~ 16 | str2 17prog03.c:21:22: note: each undeclared identifier is reported only once for each function it appears in 18prog03.c:21:18: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast [-Wint-conversion] 19 21 | strcpy(newstr[i],str[i]); 20 | ~~~~~~^~~ 21 | | 22 | char 23prog03.c:21:18: note: expected ‘char *’ but argument is of type ‘char’ 24prog03.c:23:15: error: subscripted value is neither array nor pointer nor vector 25 23 | newstr[i][0] = str2[i][0] + 32; 26 | ^ 27prog03.c:25:14: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat=] 28 25 | printf("%s\n", newstr[i]); 29 | ~^ ~~~~~~~~~ 30 | | | 31 | char * int 32 | %d 33

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dodox86

2020/10/26 11:01

何か既視感ある問題だなと思ったら、以下の質問と同じ課題っぽいですね。 入力した単語に空白を入れて文章を作る方法が分かりません https://teratail.com/questions/300085 同じ組織の、違う方からの質問なのでしょうか。(過去、そのようなケースは何度かありました)
guest

回答2

0

ベストアンサー

次のコードを理解できますか?
疑問点をコメントに書いてください。

C

1#include <stdio.h> // printf, scanf 2#include <string.h> // strcpy, strlen // (7) 3 4#define NUM 4 // (1) 5 6int main(void) 7{ 8 char words[NUM][20]; // (2) 9 char newstr[81]; // (3) 10 11 printf("Input %d words:\n", NUM); 12 for (int i = 0; i < NUM; i++) 13 scanf("%19s", words[i]); 14 15 int j = 0; 16 for (int i = 0; i < NUM; i++) { 17 strcpy(newstr + j, words[i]); // (7) 18 j += strlen(words[i]); // (7) 19 newstr[j++] = ' '; // (4) 20 } 21 newstr[j-1] = '.'; // (5) 22 newstr[j] = '\0'; 23 printf("%s\n", newstr); // (6) 24}

実行結果

text

1Input 4 words: 2this 3is 4an 5apple 6this is an apple.

投稿2020/10/26 10:01

kazuma-s

総合スコア8224

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

cuttman_bro

2020/11/01 09:26

いえ、理解できました!ありがとうございます!
guest

0

#include ..........

これがダメです。
ちゃんとインクルードするヘッダファイルを書きましょう

それから、

strcpy(newstr[i],str[i]);

str が未定義です

newstr[i][0] = str2[i][0] + 32;

newstr の型がこの書き方とはあってません

投稿2020/10/21 08:41

編集2020/10/21 08:45
y_waiwai

総合スコア87747

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

cuttman_bro

2020/10/21 08:51

実際のコードをどう変えればいいのか教えてください
y_waiwai

2020/10/21 08:55

それはあなたが自分で学び、あなたが自分で考えるものです。 がんばってください
fana

2020/10/21 08:57

> #include .......... そもそもこんな謎の文字列をソースに記述したその動機が謎すぎる.
退会済みユーザー

退会済みユーザー

2020/10/22 00:12

見た感じ、要件が学校の課題っぽいですね。 1-7見て、適切なインクルードファイルに置き換えろよってのを意図してるんでしょう(多分)
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問