Saturday, 2 June 2012

C++ Program to demonstrate reversing of an input text using a function with string arguments

C++ Program  to demonstrate reversing of  an input text using a function with string arguments

#include <stdio.h>
#include <iostream.h>
#include<conio.h>
/* strlen - return length of string s
 */
int strlen(char s[])
    {
    int i;
    for (i = 0; s[i] != '\0'; ++i);
    return (i);
    }

/* reverse - reverses order of characters in a string
 */
void reverse(char s[])
    {
    char t;
    short i, j;

    for (i = 0, j = strlen(s) - 1; i < j; ++i, --j)
    t = s[i], s[i] = s[j], s[j] = t;
    }

main()
    {
    char line[BUFSIZ];  /* the line of input text */

    cout<<endl;
    cout<<"Input String to obtain in reverse");
    cin.getline(line,BUFSIZ);
    reverse(line);
    cout<<endl;
    cout<<line;
    getch();
    }

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More