反转C中的字符串 [英] Reversing strings in C
本文介绍了反转C中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何编写一个用于显示反向字符串的ac程序,以显示每个字符之间的字符"x",例如:(oxlxlxexh)
how to write a c program for display reverse string with characters "x" in between each character eg:(oxlxlxexh)
推荐答案
对于初学者... C没有不是真的有字符串...字符串是字符数组,应该使反转更简单(直观).
考虑一下(伪代码)...
For starters... C doesn''t really have strings... strings are character arrays, which should make reversing even simpler (intuitively).
Think about it (pseudo-code)...
char word[]="hello";
/*
so the characters...
*/
word[0] = ''h'', word[1] = ''e'', word[2] = ''l'', word[3] = ''l'', word[4] = ''o''
/*
if you want to reverse that, what''s the simplest way? ...::cough cough:: _index_ ::cough::
*/
word[4] = ''o'', word[3] = ''l'', ...
/*
Of course, you can add loops to make that more efficient code
Now to add the ''x'' character, well, that''s easy too... just transfer onto a larger array and add those extra characters into it... to make it a bit more flexible, look at dynamic memory allocation of character arrays using malloc() and free()
*/
这确实是一个简单的(我想说是基本的)任务.您只需要阅读有关C
编程语言的精彩教程并开始编码.然后,如果您有任何问题,请随时在此处发布特定问题.
This is really a simple (I would say basic) task. You have just to read a good tutorial about C
programming language and start coding. Then, if you have any problem feel free to post here specific questions here.
这篇关于反转C中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文