Skip to content

Commit b1686b2

Browse files
committed
Add blog post palindrome string, palindrome number
1 parent cc77da8 commit b1686b2

4 files changed

+294
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
layout: post
3+
title: "Difference Between C++ and Java"
4+
author: gaurav
5+
categories: [ Java, Core Java ]
6+
featured: false
7+
toc: true
8+
description: "In this article, we will see the difference between C++ and Java."
9+
---
10+
11+
In todays programming world both C++ and Java are popular.
12+
13+
Both are the Object Oriented Programming languages. Still, C++ and Java has different set of features for different use-cases.
14+
15+
Let's try to understand the basic difference between C++ and Java.
16+
17+
## Difference between C++ and Java (C++ vs Java)
18+
19+
| Property | C++ | Java |
20+
| ------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |
21+
| Platform Independence | C++ is platform dependent programming language. | C++ is platform independent programming language. |
22+
| Garbage Collection | Programmer has to take care of the Garbage Collection. I.e. Removing unrefereed object from the memory. | Java has a provision to remove unreferred objects from the memory. i.e. Garbage Collection |
23+
| Multiple Inheritance | C++ supports multiple inheritance. | Java does not support multiple inheritance. |
24+
| Internal access to Memory | C++ has concept of 'Pointers' to access the internal memory. | Java does not have Pointers. |
25+
| Object Oriented | C++ is less object oriented as we can write structural programs without using objects. | Java is more purer Object Oriented Language as compare to C++ (except for primitive variables). |
26+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
---
2+
layout: post
3+
title: "Java Program To Check If the given String Is A Number or Not"
4+
author: gaurav
5+
categories: [Java Programs, Core Java]
6+
toc: true
7+
description: "In this tutorial, we will see a Java program to check if the given String is number or not."
8+
---
9+
10+
In this tutorial, we will see a Java program to check if the given String is number or not.
11+
12+
We can check it using the regex expression.
13+
14+
## Java Program To Check If the Given String is Number or Not Using Regex Expression
15+
16+
In the below program we have simply used the regex expression to check whether the given string is numeric or not.
17+
18+
The regex expression to check this is `-?\\d+`
19+
20+
Here,
21+
22+
`-?` --> negative sign, could have none or one
23+
24+
`\\d+` --> one or more digits
25+
26+
**Java Program**
27+
28+
```java
29+
import java.util.List;
30+
31+
/**
32+
* A Java program to check if the given String is number of not.
33+
* @author coderolls.com
34+
*/
35+
public class CheckIfNumber {
36+
37+
public static void main(String[] args) {
38+
List<String> strings = List.of("95965", "Jack485", "-9859", "784Will", "056", "Google");
39+
for(String str : strings) {
40+
boolean isNumber = isNumberOrNot(str);
41+
if (isNumber) {
42+
System.out.println("The String '" + str + "' is a Number.");
43+
} else {
44+
System.out.println("The String '" + str + "' is not a Number.");
45+
}
46+
}
47+
48+
}
49+
50+
/**
51+
* A method check if the input param string is a numeric or not
52+
* it is uses a regex Expression
53+
* -? --> negative sign, could have none or one
54+
* \\d+ --> one or more digits
55+
* @param str
56+
* @return
57+
*/
58+
private static boolean isNumberOrNot(String str) {
59+
return str.matches("-?\\d+");
60+
}
61+
}
62+
```
63+
64+
Output
65+
66+
```
67+
The String '95965' is a Number.
68+
The String 'Jack485' is not a Number.
69+
The String '-9859' is a Number.
70+
The String '784Will' is not a Number.
71+
The String '056' is a Number.
72+
The String 'Google' is not a Number.
73+
```
74+
75+
76+
77+
## Java Program To Check If the Given String is Number or Not Using the `Character.isDigit()` method
78+
79+
In this method we are iterating over an array of characters of the String and checking if any character is a digit or not.
80+
81+
If we found one or more digit in the string, the given string is not a number string or a number.
82+
83+
```Java
84+
import java.util.List;
85+
86+
/**
87+
* A Java program to check if the given String is number of not.
88+
* using the Character.isDigit() method.
89+
* @author coderolls.com
90+
*/
91+
public class CheckIfNumber2 {
92+
93+
public static void main(String[] args) {
94+
List<String> strings = List.of("95965", "Jack485", "-9859", "784Will", "056", "Google");
95+
for(String str : strings) {
96+
boolean isNumber = isNumberOrNot(str);
97+
if (isNumber) {
98+
System.out.println("The String '" + str + "' is a Number.");
99+
} else {
100+
System.out.println("The String '" + str + "' is not a Number.");
101+
}
102+
}
103+
104+
}
105+
106+
/**
107+
* A method check if the input param string is a numeric or not
108+
* it is uses a regex Expression
109+
* -? --> negative sign, could have none or one
110+
* \\d+ --> one or more digits
111+
* @param str
112+
* @return
113+
*/
114+
private static boolean isNumberOrNot(String str) {
115+
char[] chars = str.toCharArray();
116+
for(int i=0; i<str.length(); i++) {
117+
118+
char ch = str.charAt(i);
119+
if(i==0 && ch == '-') {
120+
continue;
121+
}
122+
123+
if (! (Character.isDigit(ch))) {
124+
return false;
125+
}
126+
}
127+
//If not a single ch is digit, the string is numeric
128+
return true;
129+
}
130+
}
131+
```
132+
133+
Output
134+
135+
```
136+
The String '95965' is a Number.
137+
The String 'Jack485' is not a Number.
138+
The String '-9859' is a Number.
139+
The String '784Will' is not a Number.
140+
The String '056' is a Number.
141+
The String 'Google' is not a Number.
142+
```
143+
144+
145+
146+
## Java Program To Check If the Given String is Number or Not `NumberFormatExpression` way.
147+
148+
We can also check if it using the `NumberFormatExpression` way.
149+
150+
Let's See how. (**Not recommended to use, Just for Info**)
151+
152+
We can try to parse the given String using the `Integer.parseInt(str)` method.
153+
154+
If the given string is not a number the method will throw `NumberFormatExpression` and we can return `false` by catching this expression. Otherwise `true`.
155+
156+
See the sample method below.
157+
158+
```java
159+
public static boolean isInteger(String s) {
160+
try {
161+
Integer.parseInt(s);
162+
} catch(NumberFormatException e) {
163+
return false;
164+
} catch(NullPointerException e) {
165+
return false;
166+
}
167+
// only got here if we didn't return false
168+
return true;
169+
}
170+
```
171+
172+
**But this way is not recommended as it causes expensive exception in the code.**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
layout: post
3+
title: "Java Program To Check If the given String Is Palindrome or Not"
4+
author: gaurav
5+
categories: [Java Programs, Core Java]
6+
toc: true
7+
description: "In this tutorial, we will see a Java program to check if the given String is palindrome or not."
8+
---
9+
10+
In this tutorial, we will see a Java program to check if the given String is palindrome or not.
11+
12+
## Java Program To Check If the given String Is Palindrome or Not
13+
14+
```java
15+
/**
16+
* A java program to check if the gioven string is palindrome or not.
17+
* @author coderolls.com
18+
*/
19+
public class PalindromeString {
20+
21+
public static void main(String[] args) {
22+
String str1 = "racecar";
23+
checkIfPalindrome(str1);
24+
}
25+
26+
private static void checkIfPalindrome(String str) {
27+
String reversedString = "";
28+
int len = str.length();
29+
for (int i = (len - 1); i >= 0; --i) {
30+
reversedString = reversedString + str.charAt(i);
31+
}
32+
if (str.equalsIgnoreCase(reversedString)) {
33+
System.out.println("The String '" + str + "' is a Palindrome String.");
34+
} else {
35+
System.out.println("The String '" + str + "' is not a Palindrome String.");
36+
}
37+
}
38+
}
39+
```
40+
41+
Output
42+
43+
```
44+
The String 'racecar' is a Palindrome String.
45+
```
46+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
layout: post
3+
title: "Java Program To Check If the given Number Is Palindrome or Not"
4+
author: gaurav
5+
categories: [Java Programs, Core Java]
6+
toc: true
7+
description: "In this tutorial, we will see a Java program to check if the given Number is palindrome or not."
8+
---
9+
10+
In this tutorial, we will see a Java program to check if the given Number is palindrome or not.
11+
12+
## Java Program To Check If the given Number Is Palindrome or Not
13+
14+
```java
15+
/**
16+
* A java program to check if the given number is a palindrome or not.
17+
* @author coderolls.com
18+
*/
19+
public class PalindromeNumber {
20+
21+
public static void main(String[] args) {
22+
int num = 125521;
23+
checkIfPalindrome(num);
24+
}
25+
26+
private static void checkIfPalindrome(int num) {
27+
int numCopy = num;
28+
int reversedNumber = 0;
29+
int reminder;
30+
31+
while(numCopy != 0){
32+
reminder = numCopy % 10;
33+
reversedNumber = reversedNumber *10 + reminder;
34+
numCopy /=10;
35+
}
36+
if (reversedNumber == num) {
37+
System.out.println("The Number '" + num + "' is a Palindrome Number.");
38+
} else {
39+
System.out.println("The Number '" + num + "' is not a Palindrome Number.");
40+
}
41+
}
42+
}
43+
```
44+
45+
Output
46+
47+
```
48+
The Number '125521' is a Palindrome Number.
49+
```
50+

0 commit comments

Comments
 (0)