여기서 우리는 미리 정의된 기능 없이 문장을 뒤집는 프로그램을 작성합니다. for 루프를 사용하면 명령문을 역순으로 쉽게 인쇄할 수 있습니다.
프로그램 1
#include<stdio.h>
int main(){
char stmt[100];
int i;
printf("enter the message:\n");
for(i=0;i<stmt;i++){
stmt[i]=getchar(); //reading each char from console till enter or newline char is pressend
if(stmt[i]=='\n')
break;
}
printf("the reverse statement is:\n");
for(i--;i>=0;i--) //printing each char in reverse order
putchar(stmt[i]);
putchar('\n');
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