Sunday, January 30, 2011

A simple loop in C++ ( cpp Loop Program)

Loops are actually used to repeat the statements to a specified number of times.

Here is a simple loop program to add numbers between 2 inputted numbers.

#include"iostream.h"
#include"conio.h"

void main()
{
clrscr();
int num1,num2,sum=0;

cout<<"Enter 1st number :: ";
cin>>num1;

cout<<"\nEnter 2nd number :: ";
cin>>num2;

for(int i=num1;i<=num2;i++)
{
sum=sum+i;
cout<<"\nSum of the numbers is "<getch();
}

Saturday, January 29, 2011

Turbo c compiler download

Want to download Turbo C compiler ?

Here is a link

C Compiler download

Reversing words in a string in cpp (c plus plus)

Now , we wanna do something chilly , user will enter a string as
i am a boy
our program should return
boy a am i

Here is a program for this logic . .

******************* Start of Program ************************

// Reversing words in a string

#include"iostream.h"
#include"conio.h"
#include"stdio.h"

void main()
{
clrscr();
char str1[50],str2[50];
cout<<"Enter a String :: ";
gets(str1);

int h=0;
while(str1[h]!='\0') // to get number of chracters in sub string
{
h++;
}

int flag=h-1;
int u=0;


int d=0;
for(int i=h-1;i>=0;i--)
{
if(str1[i]==' '||i==0)
{
u=i;
while(u<=flag)
{
str2[d]=str1[u];
d++;
u++;
}
if(i!=0)
{
str2[d]=' ';
d++;
}
flag=i-1;
}
} str2[d]='\0';

cout<<"\n\nProcessed string is :: "<<(str2);
getch();
}

Reverse a string in c++

Here is a program to reverse a string , quite interesting and easy

******************* Start of Program ************************


// Reversing a string

#include"iostream.h"
#include"conio.h"
#include"stdio.h"

void main()
{
clrscr();
char str1[50],str2[50];
cout<<"Enter a String :: ";
gets(str1);

int h=0;
while(str1[h]!='\0') // to get number of chracters in sub string
{
str2[h]=str1[h];
h++;
}
str2[h]='\0';

int k=0,m=h-1;

while(m>=0)
{
str1[k]=str2[m];
m--;
k++;
}


cout<<"\n\nOutput string is :: "<
getch();
}

******************* END ************************

Finding sub-string in a string in c++

The main goal of this program is to find a sub string in an inputted string
for example you give an "input hello i am a good boy" , and gives sub-string hello, it will find hello in the string that whether its there or not



******************* Start of Program ************************

// Finding sub-string in a String

#include"iostream.h"
#include"conio.h"
#include"stdio.h"

void main()
{
clrscr();
char str1[20],str2[20];
cout<<"Enter Main String :: ";
gets(str1);
cout<<"Enter sub string :: ";
gets(str2);

int i=0,j=0;

/////////

int h=0;
while(str2[h]!='\0') // to get number of chracters in sub string
{
h++;
}
//h-=1;

/////////



//while(str2[i]!='\0')
//{
// j=i;
int g=0;
int count=0; // to store number of matches

int c=0;

int flag;


while(str1[g]!='\0')
{

if(count==h)
break;

else
{

if(str2[c]==str1[g])
{
if(count==0)
flag=g;
count++;
c++;
}

else if(count>0)
{
c=0;
count=0;
g=flag;
}

else
{
// c++;
}

}

g++;

}



if(count==h)
{
cout<<"\n\nSub String Matched";
}
else
cout<<"\n\nSub String was not found";

getch();
}

******************* END ************************