MENU











Tuesday, 25 February 2014

JAVA 2-2

Write a Java Program for sorting a given list of names in ascending order

PROGRAM: (using sort method)

Output:



C:\Users\Personal\Desktop\JAVA>java name

Enter The number of Names :5

Enter Name 1 : blackberry
Enter Name 2 : samsung
Enter Name 3 : nokia
Enter Name 4 : apple
Enter Name 5 : sony

Names in Ascending Order

apple
blackberry
nokia
samsung
sony


Tuesday, 18 February 2014

JAVA

PROGRAM: Prime numbers

Output:

Enter the range to find prime numbers 100 The prime numbers below 100are 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

JAVA

PROGRAM:fibonacci

Output:

Enter the index to find fibonacci value 5 fibonacci number at index5 is 5

Wednesday, 29 January 2014

Friday, 17 January 2014

oops week 20

Week 20 solutions

20a) Write a C++ program to write a function void to_lower(char* s) 
that replaces all uppercase characters. 
Don't use any standard library functions.

Program:

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
void to_lower(char* s);
int main()
{
 string st;
 char* s;
 cout<<"Enter any string\n";
 cin>>st;
 s = strdup(st.c_str());
 
 to_lower(s);
 cout<<"\n Original string is :"<<st;
 cout<<"\n modified string is :"<<s; 
 return 0;
}
void to_lower(char* s)
{
 for(int i=0;s[i]!='\0';i++)
  s[i] = (s[i]>='A' && s[i]<='Z')?(s[i]+32):s[i];
}
output:


Enter any string
C++PROGRAMMING

 Original string is :C++PROGRAMMING
 modified string is :c++programming


  


20b) b) Write a C++ program to write a function,char* findx(const char* s, const char* x), 
that finds the first occurrence of the string x in s.

Program:

#include<iostream>
#include<cstring>
#include<cctype>
using namespace std;

char* findx(const char* s,const char* x);

int main()
{
 string st,st1;
 char* a;
  char* b;
 cout<<"Enter any string\n";
 getline(cin,st);
 cout<<"Enter string to search\n";
 cin>>st1;
 a = strdup(st.c_str());
 b = strdup(st1.c_str());
 
 findx(a,b);
 
 return 0;
}
char* findx(const char* s,const char* x)
{
 char* check=new char[10];
 int j=0;
 int count=0;
 for(int i=0;i<=strlen(s);i++)
 {
  if(isspace(s[i])||s[i]=='\0')
  {
   check[j]='\0';
   j=0;
      if(strcmp(check,x)==0)
      {
    cout<<x<<" is present in the string at index :"<<i-strlen(x)<<endl;
    count++;
    
             }
  }
  else
   check[j++]=s[i];
  
 }
 
 if(count==0)
  cout<<x <<" is not present in the string \n";
  
}

output: 
 
Enter any string
Ramu is a naughty boy and he is also intelligent
Enter string to search
is
is is present in the string at index :5
is is present in the string at index :29

C:\Users\Personal\Desktop>



oops week 21

Week 21 solutions

21a) Write a C++ program that reads characters from cin into an array 
that you allocate on thefree store. Read individual characters until 
an asterisk (*) is entered. Do not use a std::string.
Program:

#include<iostream>
using namespace std;
int main()
{
 int max = 10;           // no longer const
 char* a= new char[max]; //  allocated on heap
 int n = 0;

 cout<<"Enter the characters of an array\n";   
 while (cin >> a[n]&&a[n]!='*')    //--- Read into the array
 {
  n++;
 }
 cout<<"Contents in the array are:\n";
 for (int i=0; i<n; i++) 
  cout<<a[i]<<"  ";
}

output:


Enter the characters of an array
abcdefg*

Contents in the array are:
a  b  c  d  e  f  g




21b) Write a C++ program to write a function, char* strdup(const char*) that copies a string
into memory it allocates on the free store. Use the dereference operator * instead.

Program:

#include<iostream>
#include<cstring>
#include<cstdlib>
using namespace std;
char* strdup(const char* s)
{
 size_t len = 1+strlen(s);
 char *p = (char*)malloc(len);
 memcpy(p, s, len);
 return p ;
}

int main()
{
 char* n=new char[20];
 char* copy;
 cout<<"enter a string\n";
 cin>>n;
 copy=strdup(n);
        cout<<"Copied string is :  "<<copy<<endl;
 return 0;
}
output: 
 


enter a string
HelloWorld
Copied string is :  HelloWorld




oops week 22

Week 22 solutions

22a) a) Write a C++ program to write a function 
char* findx(const char* s, const char* x) that
find the first occurrence of the string x in s.
 Use dereference operator * instead.

   
   Same as 20a

  


22b) Write a C++ program to write a function 
string cat_dot(const string& s1, const string&s2), 
that concatenates two strings with a dot in between.

program:

#include<iostream>
#include<string.h>
using namespace std;

string cat_dot(const string&s1,const string&s2);
int main()
{
 string s1,s2,s3;
 cout<<"Enter string 1\n";
 cin>>s1;
 cout<<"Enter string 2\n";
 cin>>s2;
 s3=cat_dot(s1,s2);
 cout<<"After concatenation:\n";
 cout<<s3;
 
}
string cat_dot(const string&s1,const string&s2)
{
 string a;
 a=s1+"."+s2;
 return a;
}

output: 


Enter string 1
C++
Enter string 2
Programming
After concatenation:
C++.Programming





22c) Write a template function that adds a vector of elements of an 
object of any type to which elements can be added.
program:

#include<iostream>
#include<vector>
using namespace std;
template<class T>
T sum(vector<T>v) 
{
   T result;
   for (int i = 0; i < v.size(); ++i)
      result+=v[i];
  
 return result;
}
int main()
{
 
 vector<int>v;
 int x,res;
 cout<<"Enter the elements of vector\n";
 while(cin>>x)
 {
  v.push_back(x);
 }
 res=sum(v); 
 cout<<"sum of all elements is :"<<res<<endl;
}

output: 


Enter the elements of vector
1 2 3 4 5 6 7 8 9 10 11 11 *
sum of all elements is :77