"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Thursday, February 18, 2010

Write a program to reverse a string (i) using another array, (ii) without another array.


#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void main()
{

int str[50], rev[50];

cout<< "\n Enter a string :";

 gets(str);
int l = strlen(str);

/* Reverse String using another array */
for( int i = l-1, k = 0 ; i>= 0 ; i--, k++)
rev[k] = str[i];

 rev[k] = '\0';

cout<< "\n Original String is "<< str;
cout<< "\n Reverse String is "<< rev ;
cout<< "\n Reverse String is ";

/* Reverse String without using another array */
for(  i = l-1 ; i>=0 ; i--)
 cout<< str[i];

 getch();

}

Write a program to count the number of words in a string as well as the number of characters other than a space.


#include<iostream.h>
#include<conio.h>
#include <stdio.h>
#include <string.h>

void main()
{
char sen[50];
int word_count = 0, chr = 0, space = 0;

cout<< "\n Enter A String :";

gets(sen);

for(int i = 0; sen[i] != '\0' ; i++)
{ if (sen[i] == ' ')
 space++; }

chr = strlen(sen) - space;
 word_count = space + 1;

cout<< "\n The Total no. of words = " << word_count;

cout << "\n The total characters without spaces = " << chr;

 getch();
}
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.