Post ur C/C++ Programs Here

mavihs

Adept
Hey guys,

If you are a good at C/C++ Programming or if you are a programmer or just know this language then post you Programs here. By this way it helps learners a lot. Members can post their programs and get suggestions if there is anything wrong it.
 
Here is 1 from me:

Code:
/*WAP to copy content of one file to another
i) As it is.
ii) After converting all characters to upper case

Name: Shivam*/

#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<process.h>
#include<stdlib.h>

void main()
{
   clrscr();
   char path1[100], path2[100], ch, ch2;
   int cho;
   cout<<"\n\tEnter the path of the file which has to be copied: ";
   gets (path1);
   ifstream org(path1);
   if(!org)
   {
      cout<<"\tFile not found!";
      exit(0);
   }
   cout<<"Enter the name or the path of the file where the file has to be copied to: ";
   gets(path2);
   ofstream wrt(path2);
   if(!wrt)
   {
      cout<<"\tCannot create file!";
      exit(0);
   }

   cout<<"\t 1. Enter 1 to copy file as it is. \n\t 2. Enter 2 to copy file after converting all lower case characters to upper case characters. \n\n\t Enter your choice: ";
   cin>>cho;
   if(cho==1)
   {
      do{
         org.get(ch);
         wrt.put(ch);
      }while(!org.eof());
   }
   else
   {
      if(cho==2)
      {
         do{
            org.get(ch);
            if(ch == 'a' || ch == 'b' || ch == 'c' || ch == 'd' || ch == 'e' || ch == 'f' || ch == 'g' || ch == 'h' || ch == 'i' || ch == 'j' || ch == 'k' || ch == 'l' || ch == 'm' || ch == 'n' || ch == 'o' || ch == 'p' || ch == 'q' || ch == 'r' || ch == 's' || ch == 't' || ch == 'u' || ch == 'v' || ch == 'w' || ch == 'x' || ch == 'y' || ch == 'z')
            {
               ch2 = ch - 32;
               wrt.put(ch2);
            }
            else
            wrt.put(ch);
            
         }while(!org.eof());
      }
      else
      cout<<"Wrong choice entered!";
   }
}
 
Umm why this??

if(ch == 'a' || ch == 'b' || ch == 'c' || ch == 'd' || ch == 'e' || ch == 'f' || ch == 'g' || ch == 'h' || ch == 'i' || ch == 'j' || ch == 'k' || ch == 'l' || ch == 'm' || ch == 'n' || ch == 'o' || ch == 'p' || ch == 'q' || ch == 'r' || ch == 's' || ch == 't' || ch == 'u' || ch == 'v' || ch == 'w' || ch == 'x' || ch == 'y' || ch == 'z')

why not ch > 96 && ch <123 ??
 
VanishingNerd said:
Umm why this??
if(ch == 'a' || ch == 'b' || ch == 'c' || ch == 'd' || ch == 'e' || ch == 'f' || ch == 'g' || ch == 'h' || ch == 'i' || ch == 'j' || ch == 'k' || ch == 'l' || ch == 'm' || ch == 'n' || ch == 'o' || ch == 'p' || ch == 'q' || ch == 'r' || ch == 's' || ch == 't' || ch == 'u' || ch == 'v' || ch == 'w' || ch == 'x' || ch == 'y' || ch == 'z')

why not ch > 96 && ch <123 ??

as he is also learning every one has its own way of writing a program.
 
this is not my program

Code:
main(argc, argv)
int	argc;
char	**argv;
{
	while (*argv != argv[1] && (*argv = argv[1]) && (argc = 0) || (*++argv
		&& (**argv && ((++argc)[*argv] && (**argv <= argc[*argv] ||
		(**argv += argc[*argv] -= **argv = argc[*argv] - **argv)) &&
		--argv || putchar(**argv) && ++*argv--) || putchar(10))))
		;
}

The inputs get printed character by character in alphabetical order.

Copyright (c) 1988, Landon Curt Noll & Larry Bassel.
All Rights Reserved. Permission for personal, educational or non-profit use is
granted provided this this copyright and notice are included in its entirety
and remains unaltered. All other uses must receive prior permission in writing
from both Landon Curt Noll and Larry Bassel.
source http://www.cise.ufl.edu/~manuel/obfuscate/litmaath.hint
Obfuscated C Code

interested people should check out this site
The International Obfuscated C Code Contest
 
@mahvis: bad bad idea... I agree with keane on this. I have seen many people posting in forums to get their assignment answers. I dont think I would help such people.

btw, can you write the same program that you wrote above without using the if condition? :bleh:
 
@cool1: yes i know both of them , but won't share the code.. rather i can give u some hint how to do it.

u can do both using 2 loops only.. give it a shot and get back with what u tried...
 
VanishingNerd said:
Umm why this??
why not ch > 96 && ch <123 ??
Umm why this??
Why not (ch >= 'a' && ch <= 'z')??

You're familiar with the hazards of using magic numbers and non-portable character sets, aren't you? (Hint: ASCII and EBCDIC)
 
VanishingNerd said:
Umm why this??
if(ch == 'a' || ch == 'b' || ch == 'c' || ch == 'd' || ch == 'e' || ch == 'f' || ch == 'g' || ch == 'h' || ch == 'i' || ch == 'j' || ch == 'k' || ch == 'l' || ch == 'm' || ch == 'n' || ch == 'o' || ch == 'p' || ch == 'q' || ch == 'r' || ch == 's' || ch == 't' || ch == 'u' || ch == 'v' || ch == 'w' || ch == 'x' || ch == 'y' || ch == 'z')

why not ch > 96 && ch <123 ??
:ashamed: DAnM !!!! y didn't it get into my mind, it took me a long time 2 write this.!!! thanx!!!!

H@cKer said:
as he is also learning every one has its own way of writing a program.
+1
thats y i've put this tread so ppl can learn from seeing other prgs & also ppl can help existing 1's!!!
booo said:
@mahvis: bad bad idea... I agree with keane on this. I have seen many people posting in forums to get their assignment answers. I dont think I would help such people.

btw, can you write the same program that you wrote above without using the if condition? :bleh:
there r similar threads in other forum also. we can all post already made prgs & ask for help in improving existing 1's but doing other ppls assignment won't be done!!!

i think i can write without if hopefully!!! i did this prg for my skool so also posted it here.

Yamaraj said:
Umm why this??
Why not (ch >= 'a' && ch <= 'z')??

You're familiar with the hazards of using magic numbers and non-portable character sets, aren't you? (Hint: ASCII and EBCDIC)
i'm confused about this part. will it work????
cool1 said:
any 1 knows program floyd's triangle using c#
for this
1
no projects will be entertained here!!!! if u complete this prg on ur own then u can post it here or if u want a bit help u can ask for help!!!
 
From

Hello World ! - GNU Project - Free Software Foundation (FSF)

#include <iostream.h>

#include <string.h>

class string

{

private:

int size;

char *ptr;

public:

string() : size(0), ptr(new char('\0')) {}

string(const string &s) : size(s.size)

{

ptr = new char[size + 1];

strcpy(ptr, s.ptr);

}

~string()

{

delete [] ptr;

}

friend ostream &operator <<(ostream &, const string &);

string &operator=(const char *);

};



ostream &operator<<(ostream &stream, const string &s)

{

return(stream << s.ptr);

}

string &string::eek:perator=(const char *chrs)

{

if (this != &chrs)

{

delete [] ptr;

size = strlen(chrs);

ptr = new char[size + 1];

strcpy(ptr, chrs);

}

return(*this);

}

int main()

{

string str;

str = "Hello World";

cout << str << endl;

return(0);

}
 
hammerhead said:
Bad bad idea. Keane's right.
every1 has here own views!!! u feel its not right, i feel its right.
viridian said:
From

Hello World ! - GNU Project - Free Software Foundation (FSF)

#include <iostream.h>
#include <string.h>
class string
{
private:
int size;
char *ptr;
public:
string() : size(0), ptr(new char('\0')) {}
string(const string &s) : size(s.size)
{
ptr = new char[size + 1];
strcpy(ptr, s.ptr);
}
~string()
{
delete [] ptr;
}
friend ostream &operator <<(ostream &, const string &);
string &operator=(const char *);
};

ostream &operator<<(ostream &stream, const string &s)
{
return(stream << s.ptr);
}
string &string::eek:perator=(const char *chrs)
{
if (this != &chrs)
{
delete [] ptr;
size = strlen(chrs);
ptr = new char[size + 1];
strcpy(ptr, chrs);
}
return(*this);
}
int main()
{
string str;
str = "Hello World";
cout << str << endl;
return(0);
}
wat does this do????
 
program request !!!

can nybody tell me how to print a name in the below format?

eg:

ranjini

anjinir

njinira

jiniran

iniranj

niranji

iranjin

ranjini

i used 3 loops!!!

one to put the first letter to last position !

the second to left shift the whole thing !

and the last loop to print the name till it ragains its original meaning !!

it didnt work !!!

c program !!!!

and can nyone tell me the bbcode to make a code in ur post??????????
 
Try this out

Code:
# include <stdio.h>
# include <conio.h>
# include <string.h>
main()
{

char arg[20],new[20];
int i,j,k;
clrscr();

printf("Enter the string: ");
gets(arg);
printf("The value that you have entered is ---> %s",arg);

i = strlen(arg);
printf("\nThe length of the string is ---> %d", i);

for(j=0;j<=i;j++)
{

for(k=j;k<=i;k++) new[k-j] = arg[k];
for(k=0;k<j;k++) new[i-j+k] = arg[k];

printf("\n%s",new);
}

getch();

}

Here is the output

Code:
Enter the string: Ranjini
The value that you have entered is ---> Ranjini
The length of the string is ---> 7
Ranjini
anjiniR
njiniRa
jiniRan
iniRanj
niRanji
iRanjin
Ranjini

Thanks for waking up my C ...... enthusiasm
 
Back
Top