使用 for 循环打印反序字符串的 C 语言程序
下面我们编写一个程序,在没有预定义函数的情况下反转句子。使用 for 循环,我们可以轻松地按相反的顺序打印语句。
程序 1
#include<stdio.h>
int main(){
char stmt[100];
int i;
printf("enter the message:
");
for(i=0;i<stmt;i++){
stmt[i]=getchar(); //reading each char from console till enter or newline char is pressend
if(stmt[i]=='
')
break;
}
printf("the reverse statement is:
");
for(i--;i>=0;i--) //printing each char in reverse order
putchar(stmt[i]);
putchar('
');
return 0;
}输出
enter the message: Hi welcome to my world the reverse statement is: dlrow ym ot emoclew iH
程序 2
这里,我们将编写一个使用 strrev 库函数反转字符串的 C 语言程序 −
#include<stdio.h>
#include<string.h>
void main(){
//Declaring two strings//
char result[50],string[25];
//Reading string 1 and String 2//
printf("Enter String to be reversed : ");
gets(string);
//Reversing using library function//
strrev(string);
printf("The reversed string is : ");
puts(string);
}输出
Enter String to be reversed : Hi welcome to tutorials Point The reversed string is : tnioP slairotut ot emoclew iH
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP