Showing posts with label OOP. Show all posts
Showing posts with label OOP. Show all posts

Sunday, March 2, 2014

Compare two strings in java

import java.util.Scanner;
class reg2{
public static void main(String []arg){
String a2 = null;
String b2 = null;

Scanner sr = new Scanner(System.in);
System.out.print("Enter first string :: ");
a2 = sr.nextLine();
System.out.print("Enter second string :: ");
b2 = sr.nextLine();

if(scheck(a2,b2)==1)
System.out.println("strings \""+a2+"\" and \""+b2+"\" were matched");
else System.out.println("sorry , strings \""+a2+"\" and \""+b2+"\" were unmatched");
}
static int scheck(String a,String b){
int a1 = a.length();
int b1 = b.length();
int i=0,j=0;

if(a1<b1){
String temp = a;
a = b ;
b = temp ;
int t = a1;
a1 = b1 ;
b1 = t ;
}

while(j!=b1){
while(i!=a1){
if(a.charAt(i)==b.charAt(j)){
i++;
j++;

if(j==b1){
return 1;
}
}
else {
i++;
j=0;
if(i==a1)
return 0;
}
}
j++;
}


return 0;
}
}




OUTPUT







Sunday, February 23, 2014

Word Guessing Game using Java

import java.io.*;
import java.util.*;
class file4{
static int kc = 0;
public static void main(String []arg) throws IOException{
FileReader fr = new FileReader("D:\\java\\f1.txt"); 

    //the file should contain words separated by spaces !
BufferedReader br2 = new BufferedReader(fr);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Random r = new Random();

String st = br2.readLine();    

//Works for single line.Use while loop till null if file contains multiple lines.
//System.out.println(st);


String t[] = st.split(" ");
String t1= t[r.nextInt(t.length)];
char ch[] = new char[t1.length()];
char ch3[] = new char[t1.length()];
for(int j=0;j<t1.length();j++){
ch3[j]=t1.charAt(j);
}
String t2;

int i,k;

for(i=0;i<t1.length();i++){
ch[i]='-';
}

// System.out.println("\nenter ur guess");
char ch2;
String x2;
printchar(ch,t1.length());
while(true){
int j;
System.out.println("\nenter ur guess\n");
x2=br.readLine();
System.out.println();

if(scheck(x2,t1)==1)
for(j=0;j<x2.length();j++){

ch2=x2.charAt(j);
//ch2=(char)br.read();
for(k=0;k<t1.length();k++){

if(ch3[k]==ch2){
ch[k]=ch2;
}


}

}
printchar(ch,t1.length());
check2(ch,t1.length());


}
}


static void check2(char c[],int k){

int i,k1=0;
for(i=0;i<k;i++){
if(c[i]=='-'){
k1++;
}
}
kc++;
if(k1==0){
System.out.println(" is correct \nCongrats ! Guess has been completed in attempt no "+kc+"!");
System.exit(0);
}
else System.out.println("\tattempts so far :: "+kc);
//return;
//System.exit(0);
}
static void printchar(char c[],int k){
for(int i=0;i<k;i++)
System.out.print(c[i]);

}
static int scheck(String a,String b){
int a1 = a.length();
int b1 = b.length();
int i=0,j=0;

if(a1<b1){
String temp = a;
a = b ;
b = temp ;
int t = a1;
a1 = b1 ;
b1 = t ;
}

while(j!=b1){
while(i!=a1){
if(a.charAt(i)==b.charAt(j)){
i++;
j++;

if(j==b1){
return 1;
}
}
else {
i++;
j=0;
if(i==a1)
return 0;
}
}
j++;
}


return 0;
}
}





Output




Wednesday, August 28, 2013

C++ program to count member function calls

/* C++ program to count member funtion calls */

#include<iostream>
using namespace std;

class A
 {
  int data;
  static int i,j;
  public:
  void input()
  {
  cout<<"enter num : ";
  cin>>data;
  i++;
  }
  void display()
  {
  cout<<"\nnum is "<<data<<endl;
  j++;
  }
  void count()
  {
  cout<<"\nInput calls = "<<i<<"\nDisplay calls = "<<j<<endl;
  }
 };
int A :: i=0;
int A :: j=0;

int main()
 {
  A A1;
  int c;
  do
  {
  cout<<"\n1.INPUT\n2.OUTPUT\n3.COUNT\n4.EXIT\nENTER CHOICE : ";
  cin>>c;
  switch(c)
  {
  case 1:
  A1.input();
  break;
case 2:
  A1.display();
  break;
case 3:
  A1.count();
  break;
default :
return 0;
  }
  }
  while(c);
  return 0;
 }






C++ program to swap numbers between two objects of different class

 /* c++ program to swap no between two objects of diff class */

#include<iostream>
using namespace std;
class A
{
public:

 int num1;

 void input()
 {
 cout<<"\nenter the number:";
 cin>>num1;
 }
 void display()
 {
 cout<<num1<<endl;
 }
friend class B;
};
class B
{
public:

 int num2;

 void input()
{
 cout<<"\nenter the second number:";
 cin>>num2;
}
void display()
{
cout<<num2<<endl;
}
friend class A;
};
void swapping(A &A1,B &B1)
{
int temp;
temp=A1.num1;
A1.num1=B1.num2;
B1.num2=temp;
}
int main()
{
A A11;
B B11;
A11.input();
B11.input();
cout<<"number before swapping:";
A11.display();
B11.display();
swapping(A11,B11);
cout<<"number after swapping:";
A11.display();
B11.display();
return 0;
}