编写一个 C 程序,在字符串中使用 for 循环以相反的顺序打印消息
这里我们编写了一个程序,在没有预定义函数的情况下反转句子。通过使用for循环,我们可以轻松地以相反的顺序打印语句。
方案一
#include输出结果int main(){ char stmt[100]; int i; printf("enter the message:\n"); for(i=0;i =0;i--) //以相反的顺序打印每个字符 putchar(stmt[i]); putchar('\n'); return 0; }
enter the message: Hi welcome to my world the reverse statement is: dlrow ym ot emoclew iH
方案二
在这里,我们将编写一个C程序来使用strrev库函数反转字符串-
#include输出结果#include void main(){ //Declaring two strings// char result[50],string[25]; //Reading string 1 and String 2// printf("输入要反转的字符串: "); gets(string); //Reversing using library function// strrev(string); printf("反转的字符串是: "); puts(string); }
输入要反转的字符串: Hi welcome to tutorials Point 反转的字符串是: tnioP slairotut ot emoclew iH