Sunil B N
BAN USER
Replace a Substring in string using Java code follows like this:
String a = "Anonymousr How are you!";
String r = a.replace("mous","kggse");
print.i(r);
it will print: "Anonuykggse How are you!"
replace
public String replace(char oldChar,
char newChar)
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
If the character oldChar does not occur in the character sequence represented by this String object, then a reference to this String object is returned. Otherwise, a new String object is created that represents a character sequence identical to the character sequence represented by this String object, except that every occurrence of oldChar is replaced by an occurrence of newChar.
Examples:
"mesquite in your cellar".replace('e', 'o')
returns "mosquito in your collar"
"the war of baronets".replace('r', 'y')
returns "the way of bayonets"
"sparring with a purple porpoise".replace('p', 't')
returns "starring with a turtle tortoise"
"JonL".replace('q', 'x') returns "JonL" (no change)
Parameters:
oldChar - the old character.
newChar - the new character.
Returns:
a string derived from this string by replacing every occurrence of oldChar with newChar.
if you want to do for many than you can make an array instead writing separate for all checkbox try like
<input type="checkbox" name="Name[]" value="A" />Aditi<br />
<input type="checkbox" name="Name[]" value="B" />Bravo<br />
<input type="checkbox" name="Name[]" value="C" />Chinmay<br />
Php code:
<?php
$aName = $_POST['Name'];
if(empty($aName))
{
echo("You didn't select any name.");
}
else
{
$N = count($aName);
echo("You selected $N name(s): ");
for($i=0; $i < $N; $i++)
{
echo($aName[$i] . " ");
}
}
?>
Main Class will be as follows:
package tictactoe;
public class Main {
public void play() {
TicTacToe game = new TicTacToe();
System.out.println("Welcome! Tic Tac Toe is a two player game.");
System.out.print("Enter player one's name: ");
game.setPlayer1(game.getPrompt());
System.out.print("Enter player two's name: ");
game.setPlayer2(game.getPrompt());
boolean markerOk = false;
while (!markerOk) {
System.out.print("Select any letter as " + game.getPlayer1() + "'s marker: ");
String marker = game.getPrompt();
if (marker.length() == 1 &&
Character.isLetter(marker.toCharArray()[0])) {
markerOk = true;
game.setMarker1(marker.toCharArray()[0]);
} else {
System.out.println("Invalid marker, try again");
}
}
markerOk = false;
while (!markerOk) {
System.out.print("Select any letter as " + game.getPlayer2() + "'s marker: ");
String marker = game.getPrompt();
if (marker.length() == 1 &&
Character.isLetter(marker.toCharArray()[0])) {
markerOk = true;
game.setMarker2(marker.toCharArray()[0]);
} else {
System.out.println("Invalid marker, try again");
}
}
boolean continuePlaying = true;
while (continuePlaying) {
game.init();
System.out.println();
System.out.println(game.getRules());
System.out.println();
System.out.println(game.drawBoard());
System.out.println();
String player = null;
while (!game.winner() && game.getPlays() < 9) {
player = game.getCurrentPlayer() == 1 ? game.getPlayer1() : game.getPlayer2();
boolean validPick = false;
while (!validPick) {
System.out.print("It is " + player + "'s turn. Pick a square: ");
String square = game.getPrompt();
if (square.length() == 1 && Character.isDigit(square.toCharArray()[0])) {
int pick = 0;
try {
pick = Integer.parseInt(square);
} catch (NumberFormatException e) {
//Do nothing here, it'll evaluate as an invalid pick on the next row.
}
validPick = game.placeMarker(pick);
}
if (!validPick) {
System.out.println("Square can not be selected. Retry");
}
}
game.switchPlayers();
System.out.println();
System.out.println(game.drawBoard());
System.out.println();
}
if (game.winner()) {
System.out.println("Game Over - " + player + " WINS!!!");
} else {
System.out.println("Game Over - Draw");
}
System.out.println();
System.out.print("Play again? (Y/N): ");
String choice = game.getPrompt();
if (!choice.equalsIgnoreCase("Y")) {
continuePlaying = false;
}
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Main main = new Main();
main.play();
}
}
//Tic Tac toe Class
package tictactoe;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class TicTacToe {
private char[][] board = new char[3][3];
private String player1;
private String player2;
private int currentPlayer;
private char marker1;
private char marker2;
private int plays;
private BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
protected void init() {
int counter = 0;
for (int i = 0; i < 3; i++) {
for (int i1 = 0; i1 < 3; i1++) {
board[i][i1] = Character.forDigit(++counter, 10);
}
}
currentPlayer = 1;
plays = 0;
}
protected void switchPlayers() {
if (getCurrentPlayer() == 1) {
setCurrentPlayer(2);
} else {
setCurrentPlayer(1);
}
setPlays(getPlays() + 1);
}
protected boolean placeMarker(int play) {
for (int i = 0; i < 3; i++) {
for (int i1 = 0; i1 < 3; i1++) {
if (board[i][i1] == Character.forDigit(play, 10)) {
board[i][i1] = (getCurrentPlayer() == 1) ? getMarker1() : getMarker2();
return true;
}
}
}
return false;
}
protected boolean winner() {
//Checking rows
char current = ' ';
for (int i = 0; i < 3; i++) {
int i1 = 0;
for (i1 = 0; i1 < 3; i1++) {
if (!Character.isLetter(board[i][i1])) {
break;
}
if (i1 == 0) {
current = board[i][i1];
} else if (current != board[i][i1]) {
break;
}
if (i1 == 2) {
//Found winner
return true;
}
}
}
//Checking columns
for (int i = 0; i < 3; i++) {
current = ' ';
int i1 = 0;
for (i1 = 0; i1 < 3; i1++) {
if (!Character.isLetter(board[i1][i])) {
break;
}
if (i1 == 0) {
current = board[i1][i];
} else if (current != board[i1][i]) {
break;
}
if (i1 == 2) {
//Found winner
return true;
}
}
}
//Checking diagonals
current = board[0][0];
if (Character.isLetter(current) && board[1][1] == current && board[2][2] == current) {
return true;
}
current = board[2][0];
if (Character.isLetter(current) && board[1][1] == current && board[0][2] == current) {
return true;
}
return false;
}
protected String getRules() {
StringBuilder builder = new StringBuilder();
builder.append("Players take turns marking a square. Only squares \n");
builder.append("not already marked can be picked. Once a player has \n");
builder.append("marked three squares in a row, they win! If all squares \n");
builder.append("are marked and no three squares are the same, a tied game is declared.\n");
builder.append("Have Fun! \n\n");
return builder.toString();
}
protected String getPrompt() {
String prompt = "";
try {
prompt = reader.readLine();
} catch (IOException ex) {
ex.printStackTrace();
}
return prompt;
}
protected String drawBoard() {
StringBuilder builder = new StringBuilder("Game board: \n");
for (int i = 0; i < 3; i++) {
for (int i1 = 0; i1 < 3; i1++) {
builder.append("[" + board[i][i1] + "]");
}
builder.append("\n");
}
return builder.toString();
}
public int getCurrentPlayer() {
return currentPlayer;
}
public void setCurrentPlayer(int currentPlayer) {
this.currentPlayer = currentPlayer;
}
public char getMarker1() {
return marker1;
}
public void setMarker1(char marker1) {
this.marker1 = marker1;
}
public char getMarker2() {
return marker2;
}
public void setMarker2(char marker2) {
this.marker2 = marker2;
}
public int getPlays() {
return plays;
}
public void setPlays(int plays) {
this.plays = plays;
}
public String getPlayer1() {
return player1;
}
public void setPlayer1(String player1) {
this.player1 = player1;
}
public String getPlayer2() {
return player2;
}
public void setPlayer2(String player2) {
this.player2 = player2;
}
}
Divide 8 balls into:
Set A: (3)
Set B: (3)
Set C: (2)
Comparison 1: A and B
if(A=B):
Compare Set C, Keeping one ball in each Pan and find the wrong one!
else:
Either A or B has to has Odd one!
Compare two balls: B1 and B2 and
if(B1=B2): B3 is thewrong one
else: B2/ B1 will be the wrong one :)
The easiest implementation consists of a private constructor and a field to hold its result, and a static accessor method with a name like getInstance().
The private field can be assigned from within a static initializer block or, more simply, using an initializer. The getInstance( ) method (which must be public) then simply returns this instance:
// File Name: Singleton.java
public class Singleton {
private static Singleton singleton = new Singleton( );
/* A private Constructor prevents any other
* class from instantiating.
*/
private Singleton(){ }
/* Static 'instance' method */
public static Singleton getInstance( ) {
return singleton;
}
/* Other methods protected by singleton-ness */
protected static void demoMethod( ) {
System.out.println("demoMethod for singleton");
}
}
// File Name: SingletonDemo.java
public class SingletonDemo {
public static void main(String[] args) {
Singleton tmp = Singleton.getInstance( );
tmp.demoMethod( );
}
}
K&R: Kernighan and Ritchie
In computational complexity theory, the complexity class NP-complete (abbreviated NP-C or NPC) is a class of decision problems. A decision problem L is NP-complete if it is in the set of NP problems and also in the set of NP-hard problems. The abbreviation NP refers to "nondeterministic polynomial time.
IPv4: uses 32 Bits
IPv6: uses 128 Bits
#include <stdio.h>
#include<ctpye.h>
#include <conio.h>
#include <string.h>
int main()
{
int i,l1=0,l2=0;
char str1[30],str2[30];
printf("\nEnter the string1, string 2:\n");
scanf("%s%s",str1,str2);
for(i=0;i<strlen(str1);i++)
{
if(isdigit(str1[i]))
l1++;
}
for(i=0;i<(strlen(str2));i++)
{
if(isdigit(str2[i]))
l2++;
}
if(l1>l2)
printf("\n%s has more numbers",str1);
else if(l2>l1)
printf("\n%s has more numbers",str2);
if(l1==l2)
{
if(strlen(str1)>=strlen(str2))
printf("\n%s has more numbers",str1);
else
printf("\n%s has more numbers",str2);
}
printf("\npress any key to continue");
getch();
}
- Sunil B N June 07, 2015