Electrofriends

To reverse the given string

by Ranjith | December 4th, 2008.

Here is the program to reverse the given string and display. The program internally uses the logic of reversing the word.

Logic: Reversing the string includes the reversing the each and every words in it. After accepting a string from user, it calls a function “strev” with two string arguments, the source and destination. It has an iterative loop, which traces from the EOL through the begining. Each time it copies the current letter to the destination. Finally it displays the resultant string.

The same program is implemented using the string pointers.

#include<stdio.h>
#include<conio.h>
void strev(char str1[50], char str2[50]);
void main()
{
char str1[50], str2[50];
clrscr();
printf(“\n\n\t ENTER A STRING…: “);
gets(str1);
strev(str1,str2);
printf(“\n\t THE REVERSED STRING IS…: “);
puts(str2);
getch();
}
void strev(char str1[50], char str2[50])
{
int i = 0, len = 0, r = 0;
while(str1[len]!=’\0′)
len++;
for(i=len-1; i>=0; i–)
{
str2[r] = str1[i];
r++;
}
str2[r] = ‘\0′;
}
Download exe and source code here.


Share and Enjoy:
  • Digg
  • Technorati
  • StumbleUpon
  • Twitter
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Reddit
  • Mixx
  • Yahoo! Buzz
  • LinkedIn
  • NewsVine
  • RSS
  • email
Similar Posts:

Leave a Reply

Share and enjoy

    • Digg
    • Facebook
    • Technorati
    • StumbleUpon
    • Twitter
    • Reddit
    • del.icio.us
    • Yahoo! Buzz
Copyright©2009 www.electrofriends.com All Rights Reserved. Powered by Dhyeya