【あるあるbot連動企画】あるあるbotに投稿したけど採用されなかったあるある募集

main関数がないと言われます。
どこに追加すれば良いのかわかりません。
ご教授願います。


#include <stdio.h>
#include <ctype.h>
void find_palindrome(const char*);
int is_palindrome(const char*, int);
const char* find_char(const char*, char);
void find_palindrome(const char* text){
int i;
int psize;
const char* ith;
const char* hit;
for (i = 0; text[i] != '\0'; i++) {
if(!isalnum(text[i])) {
continue;
}
ith = &text[i];
hit = find_char(ith + 1, *ith);
while (hit != NULL) {
psize = hit - ith + 1;
if (is_palindrome(ith, psize)) {
while (ith < hit + 1) {
putchar(*ith);
ith++;
}
putchar('\n');
return;
}
hit = find_char(hit + 1, *ith);
}
}
}
int is_palindrome(const char* chars, int size){
int l;
int r;
for (l = 0, r = size - 1; l < r; l++, r--){
while (!isalnum(chars[l])) {
l++;
}
while (!isalnum(chars[r])) {
r--;
}
if(tolower(chars[l]) == tolower(chars[r])) {
return 0;
}
}
return 1;
}
const char* find_char(const char* str, char ch) {
int i;
for (i = 0; str[i] != '\0'; i++) {
if(tolower(ch) == tolower(str[i])){
return &str[i];
}
}
return NULL;
}

A 回答 (5件)

このプログラムがどんなプログラムなのか説明がないのでわかりませんが、


とりあえず、mainを追加してエラーにならないようにしました。
業務的に意味のある結果が得られているかどうかはわかりませんが・・・・・。
-------------------------------
#include <stdio.h>
#include <ctype.h>
void find_palindrome(const char *);
int is_palindrome(const char *, int);
const char *find_char(const char *, char);
void find_palindrome(const char *text)
{
int i;
int psize;
const char *ith;
const char *hit;
for (i = 0; text[i] != '\0'; i++) {
if (!isalnum(text[i])) {
continue;
}
ith = &text[i];
hit = find_char(ith + 1, *ith);
while (hit != NULL) {
psize = hit - ith + 1;
if (is_palindrome(ith, psize)) {
while (ith < hit + 1) {
putchar(*ith);
ith++;
}
putchar('\n');
return;
}
hit = find_char(hit + 1, *ith);
}
}
}
int is_palindrome(const char *chars, int size)
{
int l;
int r;
for (l = 0, r = size - 1; l < r; l++, r--) {
while (!isalnum(chars[l])) {
l++;
}
while (!isalnum(chars[r])) {
r--;
}
if (tolower(chars[l]) == tolower(chars[r])) {
return 0;
}
}
return 1;
}
const char *find_char(const char *str, char ch)
{
int i;
for (i = 0; str[i] != '\0'; i++) {
if (tolower(ch) == tolower(str[i])) {
return &str[i];
}
}
return NULL;
}

int main()
{
find_palindrome("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
return 0;
}
    • good
    • 1
この回答へのお礼

ご回答ありがとうございます。m(_ _)m

具体的なコードを書いて頂いてありがとうございます。

トレースしてくれたのですね。

説明不足ですみません。

このプログラムは、文字列の中から、回文(palindrome)を探して表示する関数find_palindromeです。

回答に頂いたコードで、コンパイルは無事通りました。

実行したのですが、何も起こりません。

このプログラムは、どう使うのか理解していません。

textと言うファイルがあって

ファイルの内容は、

abc0cbe、ABc0CbE、AB!c0 CbE、abc0cb1bc0cbe

で、結果が

bc0cb、Bc0Cb、B!c0 Cb、bc0cb

になるはずと想像しているのですが・・・

お礼日時:2019/01/11 22:08

void main(){


find_palindrome(text);
return 0;
}

textが宣言されていませんが
    • good
    • 1
この回答へのお礼

度々、ご回答ありがとうございます。m(_ _)m

text は宣言するものなのでしょうか?

私は、textと言うファイルがあって

ファイルの内容は、

abc0cbe、ABc0CbE、AB!c0 CbE、abc0cb1bc0cbe

で、結果が

bc0cb、Bc0Cb、B!c0 Cb、bc0cb

になるはずと想像しているのですが・・・

お礼日時:2019/01/11 22:07

>出来れば具体的にmain関数を書いて頂ければ助かります。


main関数の中でfind_palindrome関数を呼べばいいだけでは
    • good
    • 1
この回答へのお礼

お早い、ご回答ありがとうございます。m(_ _)m

変数が、悪い様な気がします(T_T)

わかりません・・・

void main(){
find_palindrome(text);
return 0;
}

お礼日時:2019/01/11 19:58

>main関数がないと言われます。


>どこに追加すれば良いのかわかりません。
どこでもいいから追加してください
    • good
    • 1
この回答へのお礼

ご回答ありがとうございます。m(_ _)m

例えばmain関数を一番下に追加して

関数を指定してみたのですが、上手く動作しないです。

main関数の中が悪いのだと思います。

出来れば具体的にmain関数を書いて頂ければ助かります。

お礼日時:2019/01/11 19:15

これらの関数は、どこから開始すれば良いのでしょう・・・・


とコンパイラが困っているのですよ

mainが無いってのは、そういう意味ですが?

コンパイラが開始地点を把握できるように記述してください
    • good
    • 1
この回答へのお礼

ご回答ありがとうございます。m(_ _)m

例えばmain関数を一番下に追加して

関数を指定してみたのですが、上手く動作しないです。

main関数の中が悪いのだと思います。

出来れば具体的にmain関数を書いて頂ければ助かります。

お礼日時:2019/01/11 19:14

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!


おすすめ情報