用C++的函数写一段代码:输入一串字符,先输出顺向字符,然后输出逆向字符。

需要用到 fill_array做补充数组吗,然后还要有show_array做顺向输出,最后是Reverse_array做逆向输出。。。。整个代码要有这3个函数在里面。。。。

太简单的逻辑了,同学要努力啊


#include <iostream.h>
#include <string.h>
int main ()
{
    char s[255];
    cin.getline(s,255);
    cout<<"asc:"<<s<<endl;
    cout<<"des:";
    for(int i=strlen(s)-1;i>=0;i--)
        cout<<s[i];
    cout<<endl;
}

追问

可以用 fill_array做补充数组吗,然后还要有show_array做顺向输出,最后是Reverse_array做逆向输出。。。。整个代码要有这3个函数在里面。。。。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-05-16
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
    char str[100] = {0};
              
    cout << "input a string:";
    gets(str);
    cout << str << endl;
    for(int i = 1; i <= strlen(str); i++)
        cout << str[strlen(str)-i];
              
    cout << endl;
    return 0;
}

简单实现

第2个回答  2013-05-16
//双向链表实现
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
char data;
struct node *next,*prior;
} list;

list *linklist()
{
list *head,*s;
char x;
head=(list*)malloc(sizeof(list));
head->next=head;
head->prior=head;
scanf("%c",&x);
while(x!='\n')
{
s=(list*)malloc(sizeof(list));
s->data=x;
s->prior=head;
s->next=head->next;
head->next->prior=s;
head->next=s;
scanf("%c",&x);
}
return head;
}

int print1(list *h)
{
list *p;
p=h->next;
while(p!=h)
{
printf("%3c",p->data);
p=p->next;
}
printf("\n");
}

int print2(list *h)
{
list *p;
p=h->prior;
while(p!=h)
{
printf("%3c",p->data);
p=p->prior;
}
printf("\n");
}

int main()
{
list *p,*h;
int i;
char x;
h=linklist();
printf("顺序输出\n");
print1(h);
printf("逆序输出\n");
print2(h);
system("pause");
}追问

可以用array么。。。。

第3个回答  2013-05-16
#include <iostream>
#include<string>
using namespace std;
int main()
{
string s;
cin>>s;
int i;
for(i=s.length()-1;i>=0;i--)
cout<<s[i];
cout<<endl;
return 0;
}
相似回答