C 编程入门 - [期末考试范例] 回文函数
Intro to Programming in C - [Final Exam Sample] Palindrome Function
我需要帮助!我做不到。我尝试了很多方法,但都没有奏效。
至少有人可以帮我解决逻辑问题。下面我将粘贴我的代码。
- 开发一个包含接收字符数组的函数的程序。该函数必须修改原件并将其准确地复制到另一个,但在每个单词前放置两个星号 (**) 是回文,回文是一个向后或向前读取相同的单词(例如 ANNA、KAYAK)。
例如,
我最好的朋友安娜有一艘红色的皮划艇
我最好的朋友**安娜有一艘红色的**皮划艇。
#include <stdio.h>
#include <stdlib.h>
void Cargar(char texto1[])
{
int i=0;
char letra;
do
{
letra=getche();
if(letra != '.')
{
texto1[i]=letra;
i++;
}
}
while (letra != '.' && i<99);
texto1[i]='[=10=]';
}
int main()
{
char texto1[100], texto2[100];
printf("Ingrese texto: ");
Cargar(texto1);
int pos=0, esp_anterior=0, aux1=0, aux2=0, aux3=0, bandera=0;
int i, j, k;
for(pos = 0 ; texto1[pos] != '[=10=]' ; pos++)
{
if( texto1[pos] == ' ')
{
if(bandera==0)
{
if( hay_palindromo(texto1, pos, esp_anterior) == 1)
{
texto2[0]='*';
texto2[1]='*';
for(i=2, j=0; j<pos ; j++ , i++)
{
texto2[i]=texto1[j];
}
aux1=i;
aux2=j;
}
else
{
for(i=0; i<pos; i++)
{
texto2[i]=texto1[i];
}
aux3=i;
}
bandera = 1;
esp_anterior = pos;
}
else
{
if(bandera == 1)
{
if( hay_palindromo(texto1, pos, esp_anterior) == 1)
{
texto2[aux1]='*';
texto2[aux1+1]='*';
for(i=aux1+2, j=aux2; j<pos ; j++ , i++)
{
texto2[i]=texto1[j];
}
aux1=i;
aux2=j;
}
else
{
for(i=aux3; i<pos; i++)
{
texto2[i]=texto1[i];
}
aux3=i;
}
esp_anterior=pos;
}
}
}
}
printf("\n%s", texto2);
return 0;
}
int hay_palindromo(char texto1[], int pos, int esp_anterior)
{
int i, j, bandera=0;
for(i=esp_anterior, j=pos; i<pos; i++, j--)
{
if(texto1[i] == texto1[j])
{
bandera=1;
}
else
{
bandera=0;
break;
}
}
if(bandera==1)
{
return 1;
}
else
{
return 0;
}
}
我建议这样的逻辑:
获取字符串,用分隔符分隔 - " " (space) 在你的情况下使用 strtok。
然后,将每个分隔的单词发送到一个函数,该函数确定该字符串是否为回文。
如果字符串是回文,为字符串+“”分配足够的space,将“”+字符串复制到分配的space中,将 'cursor' 的当前位置保存在该数组上。
伪示例:
"Anna notpali"
char *new_str;
int cursor = 0;
is_palindrome("Anna")
Yes -> new_str = malloc(strlen("Anna") + strlen("**") + 1)
strcpy(&(new_str[cursor]),"**Anna");
cursor += strlen("**Anna");
is_palindrom("notpali")
No -> new_str = realloc(new_str,strlen(" notpali") + 1)
strcpy(&(new_str[cursor])," notpali");
cursor += strlen(" notpali");
// After going through all the words
new_str[cursor] = '[=10=]'
等,可能会有一些极端情况需要处理,但这是我建议处理的基本逻辑。
我需要帮助!我做不到。我尝试了很多方法,但都没有奏效。 至少有人可以帮我解决逻辑问题。下面我将粘贴我的代码。
- 开发一个包含接收字符数组的函数的程序。该函数必须修改原件并将其准确地复制到另一个,但在每个单词前放置两个星号 (**) 是回文,回文是一个向后或向前读取相同的单词(例如 ANNA、KAYAK)。
例如, 我最好的朋友安娜有一艘红色的皮划艇
我最好的朋友**安娜有一艘红色的**皮划艇。
#include <stdio.h>
#include <stdlib.h>
void Cargar(char texto1[])
{
int i=0;
char letra;
do
{
letra=getche();
if(letra != '.')
{
texto1[i]=letra;
i++;
}
}
while (letra != '.' && i<99);
texto1[i]='[=10=]';
}
int main()
{
char texto1[100], texto2[100];
printf("Ingrese texto: ");
Cargar(texto1);
int pos=0, esp_anterior=0, aux1=0, aux2=0, aux3=0, bandera=0;
int i, j, k;
for(pos = 0 ; texto1[pos] != '[=10=]' ; pos++)
{
if( texto1[pos] == ' ')
{
if(bandera==0)
{
if( hay_palindromo(texto1, pos, esp_anterior) == 1)
{
texto2[0]='*';
texto2[1]='*';
for(i=2, j=0; j<pos ; j++ , i++)
{
texto2[i]=texto1[j];
}
aux1=i;
aux2=j;
}
else
{
for(i=0; i<pos; i++)
{
texto2[i]=texto1[i];
}
aux3=i;
}
bandera = 1;
esp_anterior = pos;
}
else
{
if(bandera == 1)
{
if( hay_palindromo(texto1, pos, esp_anterior) == 1)
{
texto2[aux1]='*';
texto2[aux1+1]='*';
for(i=aux1+2, j=aux2; j<pos ; j++ , i++)
{
texto2[i]=texto1[j];
}
aux1=i;
aux2=j;
}
else
{
for(i=aux3; i<pos; i++)
{
texto2[i]=texto1[i];
}
aux3=i;
}
esp_anterior=pos;
}
}
}
}
printf("\n%s", texto2);
return 0;
}
int hay_palindromo(char texto1[], int pos, int esp_anterior)
{
int i, j, bandera=0;
for(i=esp_anterior, j=pos; i<pos; i++, j--)
{
if(texto1[i] == texto1[j])
{
bandera=1;
}
else
{
bandera=0;
break;
}
}
if(bandera==1)
{
return 1;
}
else
{
return 0;
}
}
我建议这样的逻辑:
获取字符串,用分隔符分隔 - " " (space) 在你的情况下使用 strtok。
然后,将每个分隔的单词发送到一个函数,该函数确定该字符串是否为回文。
如果字符串是回文,为字符串+“”分配足够的space,将“”+字符串复制到分配的space中,将 'cursor' 的当前位置保存在该数组上。 伪示例:
"Anna notpali"
char *new_str;
int cursor = 0;
is_palindrome("Anna")
Yes -> new_str = malloc(strlen("Anna") + strlen("**") + 1)
strcpy(&(new_str[cursor]),"**Anna");
cursor += strlen("**Anna");
is_palindrom("notpali")
No -> new_str = realloc(new_str,strlen(" notpali") + 1)
strcpy(&(new_str[cursor])," notpali");
cursor += strlen(" notpali");
// After going through all the words
new_str[cursor] = '[=10=]'
等,可能会有一些极端情况需要处理,但这是我建议处理的基本逻辑。