这个程序怎么写

rng_nb 2017-12-17 07:32:34
//4.问题描述:Read a four words string, places it in an array of strings, and then prints out: (e.g. I am Peter Pan)
//(i) The four strings in reverse order. (e.g. Pan Peter am I)
//(ii) The four strings in the original order, but with each string backwards. (e.g. I ma reteP naP)
//(iii) The four strings in revers order with each string backwards. (e.g. naP reteP ma I)
//输入格式:
//One line contains 4 words.(each words less than 49 chars)
//输出格式:
//3 lines:
//(i) The four strings in reverse order. (e.g. Pan Peter am I)
//(ii) The four strings in the original order, but with each string backwards. (e.g. I ma reteP naP)
//(iii) The four strings in revers order with each string backwards. (e.g. naP reteP ma I)
//输入样例:
//I am Peter Pan
//输出样例:
//Pan Peter am I
//I ma reteP naP
//naP reteP ma I
问题描述大概就是输入一个字符串像上面输出样例一样输出
...全文
258 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
petrifyLo 2017-12-18
  • 打赏
  • 举报
回复
#include<iostream> #include<string> using namespace std; void temp(char &a, char &b) { char temp; temp = a; a = b; b = temp; } void temp_s(string &a, string &b) { string temp; temp = a; a = b; b = temp; } int main() { string array[4]; for (auto &c : array) { cin >> c; } //@1 for (int i = 3; i != -1; --i) { cout << array[i] << " "; } cout << endl; //@2 for (int i = 0; i != 4; ++i) { string::iterator Biter = array[i].begin(); string::iterator Eiter = array[i].end()-1; while (Eiter-Biter>1) { temp(*Biter, *Eiter); ++Biter; --Eiter; } if (Eiter - Biter == 1) { temp(*Biter, *Eiter); } } for (auto &c : array) { cout << c << " "; } cout << endl; //@3 for (int i = 3; i != -1; --i) { cout << array[i] << " "; } cout << endl; system("pause"); return 0; }
wodexiaojidan 2017-12-17
  • 打赏
  • 举报
回复
引用 11 楼 cfjtaishan 的回复:
[quote=引用 10 楼 wodexiaojidan 的回复:] [quote=引用 9 楼 cfjtaishan 的回复:] [quote=引用 7 楼 wodexiaojidan 的回复:] memcpy是字拷贝, strcpy是字节拷贝, 你说哪个效率高。而且通常为了不发生溢出,是不推荐使用strcpy的。 我是store in reverse order,你要print in reverse order,这只能说我俩策略不一样
strcpy怎么会溢出呢?你的输入是
scanf("%s%s%s%s", s[0], s[1], s[2], s[3]);
每个单词都是一个字符串,每个单词都是字符,memcpy第三个参数是要拷贝的字节个数,而不是字个数哦。 你看一下memcpy,拷贝了49个字节。那么用strcpy,对于am只需要拷贝3个字节,对于单词I只需要拷贝1个字节。请问那个效率高呢[/quote] 大哥,麻烦你看懂了再来回复好吗?memcpy的实现是按机器字长来拷贝的,汇编源码里面有。 我说strcpy会发生溢出不是针对这里,而是使用过程中经常会出现的,难道生活中你老师/领导没说让你禁止使用strcpy而要使用安全带边界的strncpy? memcpy第三个参数可以用strlen,是不是?我这里省事这样写,你还真要吹毛求疵?[/quote] 不好意思,没有老师说过禁用strcpy,工作这么多年也没有同事说过禁用strcpy,用与不用看情况,如果知道strcpy的问题,即使用了又何妨,因为知道了什么时候不应该用。那么可以用的时候为什么不能呢。 我没说你第三参数不能用49,我不是说了吗?考虑效率,要用strcpy(或strncpy);没有说不对哦,是不是看不得别人说出自己的问题,我也是过来人,但是更好的就是更好的,不是因为自己的接不接受而变化; 最后,你的程序没问题。我想告诉楼主可以在你的基础上让程序效率更高些。我们不能止于做出来了,运行没问题了,如果可我们可以考虑让程序运行的更快更稳定。[/quote]再见,不是一路人,不用回了。
自信男孩 2017-12-17
  • 打赏
  • 举报
回复
引用 10 楼 wodexiaojidan 的回复:
[quote=引用 9 楼 cfjtaishan 的回复:] [quote=引用 7 楼 wodexiaojidan 的回复:] memcpy是字拷贝, strcpy是字节拷贝, 你说哪个效率高。而且通常为了不发生溢出,是不推荐使用strcpy的。 我是store in reverse order,你要print in reverse order,这只能说我俩策略不一样
strcpy怎么会溢出呢?你的输入是
scanf("%s%s%s%s", s[0], s[1], s[2], s[3]);
每个单词都是一个字符串,每个单词都是字符,memcpy第三个参数是要拷贝的字节个数,而不是字个数哦。 你看一下memcpy,拷贝了49个字节。那么用strcpy,对于am只需要拷贝3个字节,对于单词I只需要拷贝1个字节。请问那个效率高呢[/quote] 大哥,麻烦你看懂了再来回复好吗?memcpy的实现是按机器字长来拷贝的,汇编源码里面有。 我说strcpy会发生溢出不是针对这里,而是使用过程中经常会出现的,难道生活中你老师/领导没说让你禁止使用strcpy而要使用安全带边界的strncpy? memcpy第三个参数可以用strlen,是不是?我这里省事这样写,你还真要吹毛求疵?[/quote] 不好意思,没有老师说过禁用strcpy,工作这么多年也没有同事说过禁用strcpy,用与不用看情况,如果知道strcpy的问题,即使用了又何妨,因为知道了什么时候不应该用。那么可以用的时候为什么不能呢。 我没说你第三参数不能用49,我不是说了吗?考虑效率,要用strcpy(或strncpy);没有说不对哦,是不是看不得别人说出自己的问题,我也是过来人,但是更好的就是更好的,不是因为自己的接不接受而变化; 最后,你的程序没问题。我想告诉楼主可以在你的基础上让程序效率更高些。我们不能止于做出来了,运行没问题了,如果可我们可以考虑让程序运行的更快更稳定。
wodexiaojidan 2017-12-17
  • 打赏
  • 举报
回复
引用 9 楼 cfjtaishan 的回复:
[quote=引用 7 楼 wodexiaojidan 的回复:] memcpy是字拷贝, strcpy是字节拷贝, 你说哪个效率高。而且通常为了不发生溢出,是不推荐使用strcpy的。 我是store in reverse order,你要print in reverse order,这只能说我俩策略不一样
strcpy怎么会溢出呢?你的输入是
scanf("%s%s%s%s", s[0], s[1], s[2], s[3]);
每个单词都是一个字符串,每个单词都是字符,memcpy第三个参数是要拷贝的字节个数,而不是字个数哦。 你看一下memcpy,拷贝了49个字节。那么用strcpy,对于am只需要拷贝3个字节,对于单词I只需要拷贝1个字节。请问那个效率高呢[/quote] 大哥,麻烦你看懂了再来回复好吗?memcpy的实现是按机器字长来拷贝的,汇编源码里面有。 我说strcpy会发生溢出不是针对这里,而是使用过程中经常会出现的,难道生活中你老师/领导没说让你禁止使用strcpy而要使用安全带边界的strncpy? memcpy第三个参数可以用strlen,是不是?我这里省事这样写,你还真要吹毛求疵?
自信男孩 2017-12-17
  • 打赏
  • 举报
回复
引用 7 楼 wodexiaojidan 的回复:
memcpy是字拷贝, strcpy是字节拷贝, 你说哪个效率高。而且通常为了不发生溢出,是不推荐使用strcpy的。 我是store in reverse order,你要print in reverse order,这只能说我俩策略不一样
strcpy怎么会溢出呢?你的输入是
scanf("%s%s%s%s", s[0], s[1], s[2], s[3]);
每个单词都是一个字符串,每个单词都是字符,memcpy第三个参数是要拷贝的字节个数,而不是字个数哦。 你看一下memcpy,拷贝了49个字节。那么用strcpy,对于am只需要拷贝3个字节,对于单词I只需要拷贝1个字节。请问那个效率高呢
wodexiaojidan 2017-12-17
  • 打赏
  • 举报
回复
引用 6 楼 cfjtaishan 的回复:
不好意思,我是小学毕业,英文一窍不通,但是能看懂要求,我想只要根据要求实现即可,没必要拘于方法,那种方法好实现,效率高就可以考虑使用那种方法; 另外,建议是用strcpy代替memcpy,这样效率高些,因为拷贝的数据减少了很多(起码对于example)。strcpy(或strncpy)是针对字符串拷贝的,memcpy是多多种类型进行拷贝,其中包括字符串。 [quote=引用 5 楼 wodexiaojidan 的回复:] [quote=引用 4 楼 cfjtaishan 的回复:] [quote=引用 3 楼 wodexiaojidan 的回复:]
#include <stdio.h>
#include <string.h>

void Reverse_char(char *s)
{
	int i, end = strlen(s) - 1;

	for(i = 0; i <= end / 2; ++i){
		char ch = s[i];
		s[i] = s[end - i];
		s[end - i] = ch;
	}

}

void Reverse_string(char (*s)[49], int size)
{
	int i;

	for(i = 0; i <= (size - 1) / 2; ++i){
		char p[49] = {0};
		memcpy(p, s[i], 49);
		memcpy(s[i],s[size - 1 - i], 49);
		memcpy(s[size -1 - i], p, 49);
	}
}

int main(void)
{
	char s[4][49] = {""};
	int i;

	scanf("%s%s%s%s", s[0], s[1], s[2], s[3]);

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		printf("%s ", s[i]);
	}
	printf("\n");

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		Reverse_char(s[i]);
		printf("%s ", s[i]);
	}
	printf("\n");

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		printf("%s ", s[i]);
	}
	printf("\n");

	return 0;
}
如果是输出:
Pan Peter am I 
这个不需要任何修改,下表从3到0输出即可。 所以不需要使用memcpy 因此对于逆序(单词为单位)不需要在单独做。只需要单词下表从3到0输出即可。 对于每个单词内部的逆序,可以考虑先将单词内逆序,若需要两个逆序(单词和单词内),那么需要讲单词内逆序后再讲单词从3输出到0.[/quote]看懂英文再来回复我[/quote][/quote]memcpy是字拷贝, strcpy是字节拷贝, 你说哪个效率高。而且通常为了不发生溢出,是不推荐使用strcpy的。 我是store in reverse order,你要print in reverse order,这只能说我俩策略不一样。你要计较长度的话,先用strlen怎么样?
wodexiaojidan 2017-12-17
  • 打赏
  • 举报
回复
memcpy是字拷贝, strcpy是字节拷贝, 你说哪个效率高。而且通常为了不发生溢出,是不推荐使用strcpy的。 我是store in reverse order,你要print in reverse order,这只能说我俩策略不一样
自信男孩 2017-12-17
  • 打赏
  • 举报
回复
不好意思,我是小学毕业,英文一窍不通,但是能看懂要求,我想只要根据要求实现即可,没必要拘于方法,那种方法好实现,效率高就可以考虑使用那种方法; 另外,建议是用strcpy代替memcpy,这样效率高些,因为拷贝的数据减少了很多(起码对于example)。strcpy(或strncpy)是针对字符串拷贝的,memcpy是多多种类型进行拷贝,其中包括字符串。
引用 5 楼 wodexiaojidan 的回复:
[quote=引用 4 楼 cfjtaishan 的回复:] [quote=引用 3 楼 wodexiaojidan 的回复:]
#include <stdio.h>
#include <string.h>

void Reverse_char(char *s)
{
	int i, end = strlen(s) - 1;

	for(i = 0; i <= end / 2; ++i){
		char ch = s[i];
		s[i] = s[end - i];
		s[end - i] = ch;
	}

}

void Reverse_string(char (*s)[49], int size)
{
	int i;

	for(i = 0; i <= (size - 1) / 2; ++i){
		char p[49] = {0};
		memcpy(p, s[i], 49);
		memcpy(s[i],s[size - 1 - i], 49);
		memcpy(s[size -1 - i], p, 49);
	}
}

int main(void)
{
	char s[4][49] = {""};
	int i;

	scanf("%s%s%s%s", s[0], s[1], s[2], s[3]);

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		printf("%s ", s[i]);
	}
	printf("\n");

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		Reverse_char(s[i]);
		printf("%s ", s[i]);
	}
	printf("\n");

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		printf("%s ", s[i]);
	}
	printf("\n");

	return 0;
}
如果是输出:
Pan Peter am I 
这个不需要任何修改,下表从3到0输出即可。 所以不需要使用memcpy 因此对于逆序(单词为单位)不需要在单独做。只需要单词下表从3到0输出即可。 对于每个单词内部的逆序,可以考虑先将单词内逆序,若需要两个逆序(单词和单词内),那么需要讲单词内逆序后再讲单词从3输出到0.[/quote]看懂英文再来回复我[/quote]
wodexiaojidan 2017-12-17
  • 打赏
  • 举报
回复
引用 4 楼 cfjtaishan 的回复:
[quote=引用 3 楼 wodexiaojidan 的回复:]
#include <stdio.h>
#include <string.h>

void Reverse_char(char *s)
{
	int i, end = strlen(s) - 1;

	for(i = 0; i <= end / 2; ++i){
		char ch = s[i];
		s[i] = s[end - i];
		s[end - i] = ch;
	}

}

void Reverse_string(char (*s)[49], int size)
{
	int i;

	for(i = 0; i <= (size - 1) / 2; ++i){
		char p[49] = {0};
		memcpy(p, s[i], 49);
		memcpy(s[i],s[size - 1 - i], 49);
		memcpy(s[size -1 - i], p, 49);
	}
}

int main(void)
{
	char s[4][49] = {""};
	int i;

	scanf("%s%s%s%s", s[0], s[1], s[2], s[3]);

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		printf("%s ", s[i]);
	}
	printf("\n");

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		Reverse_char(s[i]);
		printf("%s ", s[i]);
	}
	printf("\n");

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		printf("%s ", s[i]);
	}
	printf("\n");

	return 0;
}
如果是输出:
Pan Peter am I 
这个不需要任何修改,下表从3到0输出即可。 所以不需要使用memcpy 因此对于逆序(单词为单位)不需要在单独做。只需要单词下表从3到0输出即可。 对于每个单词内部的逆序,可以考虑先将单词内逆序,若需要两个逆序(单词和单词内),那么需要讲单词内逆序后再讲单词从3输出到0.[/quote]看懂英文再来回复我
自信男孩 2017-12-17
  • 打赏
  • 举报
回复
引用 3 楼 wodexiaojidan 的回复:
#include <stdio.h>
#include <string.h>

void Reverse_char(char *s)
{
	int i, end = strlen(s) - 1;

	for(i = 0; i <= end / 2; ++i){
		char ch = s[i];
		s[i] = s[end - i];
		s[end - i] = ch;
	}

}

void Reverse_string(char (*s)[49], int size)
{
	int i;

	for(i = 0; i <= (size - 1) / 2; ++i){
		char p[49] = {0};
		memcpy(p, s[i], 49);
		memcpy(s[i],s[size - 1 - i], 49);
		memcpy(s[size -1 - i], p, 49);
	}
}

int main(void)
{
	char s[4][49] = {""};
	int i;

	scanf("%s%s%s%s", s[0], s[1], s[2], s[3]);

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		printf("%s ", s[i]);
	}
	printf("\n");

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		Reverse_char(s[i]);
		printf("%s ", s[i]);
	}
	printf("\n");

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		printf("%s ", s[i]);
	}
	printf("\n");

	return 0;
}
如果是输出:
Pan Peter am I 
这个不需要任何修改,下表从3到0输出即可。 所以不需要使用memcpy 因此对于逆序(单词为单位)不需要在单独做。只需要单词下表从3到0输出即可。 对于每个单词内部的逆序,可以考虑先将单词内逆序,若需要两个逆序(单词和单词内),那么需要讲单词内逆序后再讲单词从3输出到0.
wodexiaojidan 2017-12-17
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

void Reverse_char(char *s)
{
int i, end = strlen(s) - 1;

for(i = 0; i <= end / 2; ++i){
char ch = s[i];
s[i] = s[end - i];
s[end - i] = ch;
}

}

void Reverse_string(char (*s)[49], int size)
{
int i;

for(i = 0; i <= (size - 1) / 2; ++i){
char p[49] = {0};
memcpy(p, s[i], 49);
memcpy(s[i],s[size - 1 - i], 49);
memcpy(s[size -1 - i], p, 49);
}
}

int main(void)
{
char s[4][49] = {""};
int i;

scanf("%s%s%s%s", s[0], s[1], s[2], s[3]);

Reverse_string(s, 4);
for(i = 0; i < 4; ++i){
printf("%s ", s[i]);
}
printf("\n");

Reverse_string(s, 4);
for(i = 0; i < 4; ++i){
Reverse_char(s[i]);
printf("%s ", s[i]);
}
printf("\n");

Reverse_string(s, 4);
for(i = 0; i < 4; ++i){
printf("%s ", s[i]);
}
printf("\n");

return 0;
}


wodexiaojidan 2017-12-17
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

void Reverse_char(char *s)
{
	int i, end = strlen(s) - 1;

	for(i = 0; i <= end / 2; ++i){
		char ch = s[i];
		s[i] = s[end - i];
		s[end - i] = ch;
	}

}

void Reverse_string(char (*s)[49], int size)
{
	int i;

	for(i = 0; i <= (size - 1) / 2; ++i){
		char p[49] = {0};
		memcpy(p, s[i], 49);
		memcpy(s[i],s[size - 1 - i], 49);
		memcpy(s[size -1 - i], p, 49);
	}
}

int main(void)
{
	char s[4][49] = {"I", "am", "Peter", "Pan"};
	int i;

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		printf("%s ", s[i]);
	}
	printf("\n");

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		Reverse_char(s[i]);
		printf("%s ", s[i]);
	}
	printf("\n");

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		printf("%s ", s[i]);
	}
	printf("\n");

	return 0;
}
Pan Peter am I I ma reteP naP naP reteP ma I
wodexiaojidan 2017-12-17
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <string.h>

void Reverse_char(char *s)
{
	int i, end = strlen(s) - 1;

	for(i = 0; i <= end / 2; ++i){
		char ch = s[i];
		s[i] = s[end - i];
		s[end - i] = ch;
	}

}

void Reverse_string(char (*s)[49], int size)
{
	int i;

	for(i = 0; i <= (size - 1) / 2; ++i){
		char p[49] = {0};
		memcpy(p, s[i], 49);
		memcpy(s[i],s[size - 1 - i], 49);
		memcpy(s[size -1 - i], p, 49);
	}
}

int main(void)
{
	char s[4][49] = {"I", "am", "Peter", "Pan"};
	int i;

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		printf("%s ", s[i]);
	}
	printf("\n");

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		Reverse_char(s[i]);
		printf("%s ", s[i]);
	}
	printf("\n");

	Reverse_string(s, 4);
	for(i = 0; i < 4; ++i){
		Reverse_char(s[i]);
		printf("%s ", s[i]);
	}
	printf("\n");

	return 0;
}
Pan Peter am I I ma reteP naP Pan Peter am I

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧