回文程序 [英] Palindrome Program

查看:176
本文介绍了回文程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个程序,它输入的字符串和字符串输出倒退。我已经创建了一个计算字符串中的字符数的函数。有了这些信息,我创建了把原始数组的第一个字符在新数组的结尾另一个函数。因此,当在功能循环完成,新数组包含原始数组但反向的字母。我遇到一些困难但是。我仍然持谨慎态度字符数组的,我不知道如果我正确地使用它们。有人能帮帮我吗?

 的#include<&stdio.h中GT;#定义MAXLINE 1000落后的CHAR(字符海峡[],诠释计数)
诠释计数(字符STR [])诠释主(){
    字符原稿[MAXLINE]
    INT℃;    scanf函数(%S,原稿);
    C =计数(原稿);
    的printf(%S,落后(原稿,C));    返回0;
}落后的CHAR(字符海峡[],诠释计数){
    烧焦回来[MAXLINE]
    INT I;    对于(i = 0; I< =计数; ++ I){
        回来[计数] = STR [I]
         - 计数;    返回备用;
}诠释计数(字符STR []){
    INT I;    对于(i = 0; STR [I] ='\\ 0';!++我)
        ;    返回我;
}

编辑:输入:比方说,面包车。预计产出将是navinim。有没有实际的输出呢。我得到这个错误:

  palindrome.c:8:错误:预期'=',',',',','廉政'前'ASM'或'__attribute__'


解决方案

你得到的错误是因为你的原型是错误的:

 的#include<&stdio.h中GT;#定义MAXLINE 1000落后的CHAR(字符海峡[],诠释计数)/ *丢失; * /
诠释计数(字符STR [])/ *丢失; * /诠释主(){

您也应该考虑更换计数()的strlen(3) <&string.h中GT; 头文件。标准库满是已经调试和已知的工作以及事业 - 这是值得使用它们。 (是的,有教育价值重新实施它们。但在这种情况下,是ppared与各种边界情况进行测试$ P $)。

I've created a program that takes an inputted string and outputs the string backwards. I've created a function that counts the number of characters in a string. With that information, I've created another function that puts the first character of the original array at the end of the new array. Thus, when the loop in the function has finished, the new array contains the letters of the original array but backwards. I'm running into some difficulty however. I'm still wary of character arrays and I'm not sure if I'm using them correctly. Could someone help me out?

#include <stdio.h>

#define MAXLINE 1000

char backward(char str[], int count)
int count(char str[])

int main() {
    char orig[MAXLINE];
    int c;

    scanf("%s", orig);
    c = count(orig);
    printf("%s", backward(orig, c));

    return 0;
}

char backward(char str[], int count) {
    char back[MAXLINE];
    int i;

    for(i = 0; i <= count; ++i) {
        back[count] = str[i];
        --count;

    return back;
}

int count(char str[]) {
    int i;

    for (i = 0; str[i] != '\0'; ++i)
        ;

    return i;
}

Edit: Input: Let's say "minivan". Expected output would be "navinim". There's no actual output yet. I'm getting this error:

palindrome.c:8: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’

解决方案

The error you're getting is because your prototypes are wrong:

#include <stdio.h>

#define MAXLINE 1000

char backward(char str[], int count)  /* missing ; */
int count(char str[])                 /* missing ; */

int main() {

You should also consider replacing count() with strlen(3) from the <string.h> header file. The standard library is full of utilities that have been debugged and known to work well -- it is worth using them. (Yes, there's educational value in re-implementing them. But in that case, be prepared to test with a variety of corner cases.)

这篇关于回文程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆