Skip to content

Commit 5f17733

Browse files
committed
Add blogpost for 30 Nov 2019 and fix some issues
1 parent 1f3d280 commit 5f17733

14 files changed

+227
-44
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
# codeRolls log
1+
# codeRolls blog
22

3-
[Live Demo](https://coderolls.com)
3+
[Live Demo](https://coderolls.github.io)
44

55

6-
### Copyright
6+
#### Copyright
77

88
Copyright (C) 2019 codeRolls, https://codeRolls.com
99

10-
CodeRolls is a blog about java programming language and related technologies. All post are copyright property of Guarva Kukade. If you want to read the blog post please visit coderolls.com
10+
CodeRolls is a blog about java programming language and related technologies. All post are copyright property of Guarva Kukade (@gauravkukade). If you want to read the blog post please visit coderolls.com
1111

1212
You should not copy these blogpost, you should right your own. 🙂
1313

_config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ logo: 'assets/images/logo.png'
66
favicon: 'assets/images/logo.png'
77
baseurl: ''
88
google_analytics: 'UA-12293213-1'
9-
disqus: gauravkukade
9+
disqus: 'gauravkukade'
1010
mailchimp-list: 'https://coderolls.us4.list-manage.com/subscribe/post?u=91e4893f731db2a4807928752&id=76d82df79a'
1111
include: ["_pages"]
1212
permalink: /:title/

_layouts/default.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="utf-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
66

7-
<link rel="icon" href="{{ site.baseurl }}/assets/images/logo.svg">
7+
<link rel="icon" href="{{ site.baseurl }}/assets/images/favicon.png">
88

99
<title>{{ page.title }} | {{site.name}}</title>
1010

_pages/contact-check.md

Lines changed: 0 additions & 36 deletions
This file was deleted.

_pages/contact.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ comments: false
77

88
<div class="row justify-content-between">
99
<div class="col-md-8 pr-5">
10+
1011
<p>Hi👋🏼, I am Gaurav here! Author at codeRolls.</p>
1112

1213
<p>Get in touch with us at <span class="spoiler">hello@coderolls.com</span></p>
@@ -15,9 +16,9 @@ comments: false
1516

1617
<p>Head over to our <a href="https://github.com/coderolls/coderolls.github.io">Github repository</a>!</p>
1718

18-
<p><a href="/">Visit Home</a></p>
1919
</div>
2020

21+
2122
<div class="col-md-4">
2223

2324
<div class="sticky-top sticky-top-80">

_posts/2019-11-28-java-introduction.md renamed to _posts/2019-11-29-java-introduction.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ layout: post
33
title: "Introduction to Java Technology (Language and Platform)"
44
author: gaurav
55
categories: [ Java, Core Java ]
6-
image: assets/images/4.jpg
6+
image: assets/images/2019-11-29/java-introduction.png
77
description: "Java is a general purpose computer programming language. Let's get introduce to an amazing programming language."
88
featured: true
9+
comments: true
910
hidden: true
1011
---
1112

@@ -29,6 +30,8 @@ Let’s understand Java as a programming language.
2930

3031
Java is a high-level programming language. It is a concurrent, class-based, object-oriented programming language.
3132

33+
![Java Language Feature](/assets/images/2019-11-29/java-language-feature.png)
34+
3235
We will understand Java by the following simple terms, let’s discuss one by one,
3336

3437
### **Simple:**
@@ -136,6 +139,8 @@ When we compile this file with the javac compiler, the .class file will be gener
136139

137140
Refer the following diagram,
138141

142+
![How Java Code Works](/assets/images/2019-11-29/how-java-code-works.png)
143+
139144
What was your experience when you started learning Java, share with us in the comment section.
140145

141146
If you found this article worth, please [Give me a cup of Coffee ☕](https://paypal.me/GauravKukade).
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
---
2+
layout: post
3+
title: "How To Compare Two Strings Lexicographically In Java"
4+
author: gaurav
5+
categories: [ Java, Core Java, String]
6+
description: "In this article, we will learn how to compare two strings lexicographically in java."
7+
featured: false
8+
comments: true
9+
hidden: false
10+
---
11+
12+
In this article, we will learn how to compare two strings lexicographically in java.
13+
14+
## Introduction
15+
16+
First of all, we will understand what does ‘lexicographically’ means?
17+
18+
In simple words ‘lexicographically’ means ‘alphabetically ordered’. Yes, correct. The order in which words are given in the dictionary.
19+
20+
We are going to compare two strings so we can check their lexicographical order.
21+
22+
There are two ways to compare two strings lexicographically
23+
1. Using the Java `compareTo()` method
24+
2. By creating a user-defined method
25+
26+
Let us start with the first option
27+
28+
## 1. Using the Java `compareTo()` method
29+
Java `compareTo()` method Compares two strings lexicographically, The comparison is based on the Unicode value of each character in the strings.
30+
31+
The character sequence represented by the String object is compared lexicographically to the character sequence represented by the argument string.
32+
33+
![Showing which one is the argument string](/assets/images/2019-11-30/compareto-method-string-def.png)
34+
35+
```java
36+
int compareTo(T o)
37+
where is 'T' is type and 'o' is obejct
38+
returns - a negative integer, 0 or a positive integer
39+
40+
ex. firstString.compareTo(secondString)
41+
42+
// here character sequence of the firstString is compared lexicographically with the character sequence of the secondString
43+
```
44+
`compareTo()` returns the integer (`int`) value. The possible values are a negative integer, zero or a positive integer.
45+
46+
1. If `firstString` is less than the `secondString`, it will return a negative integer.
47+
i.e `firstString` < `secondString`→ returns a negative integer
48+
49+
2. If `firstString` is equal to the `secondString` it will return zero.
50+
i.e `firstString` == `secondString` → returns zero
51+
52+
3. If `firstString` is greater than the `secondString` it will return a positive integer.
53+
i.e `firstString` > `secondString` → returns a positive integer
54+
55+
Note: Always consider ‘Argument string’ as the reference to counting form for the sign of the value.
56+
57+
Java program to check two strings `lexicographically` using Java `compareTo()` method
58+
```java
59+
/**
60+
* A Java program to compare two strings lexicographically
61+
* using compareTo() library function.
62+
*
63+
* @author Gaurav Kukade at coderolls.com
64+
*/
65+
public class CompareLexicographically {
66+
public static void main(String[] args) {
67+
68+
String firstString = "Paneer";
69+
String secondString = "Paneer";
70+
String thirdString = "Butter";
71+
String fourthString = "Cheese";
72+
73+
System.out.println("Comparing two strings lexicographically.");
74+
75+
System.out.print("\nCompairing character sequence of the firstString ("+firstString+") to the character sequence of the secondString ("+secondString+") returns: ");
76+
System.out.println(firstString.compareTo(secondString));
77+
78+
System.out.print("\nCompairing character sequence of secondString ("+secondString+") to the character sequence of thirdString ("+thirdString+") returns: ");
79+
System.out.println(secondString.compareTo(thirdString));
80+
81+
System.out.print("\nCompairing character sequence of thirdString ("+thirdString+") to the character sequence of fourthString ("+fourthString+") returns: ");
82+
System.out.println(thirdString.compareTo(fourthString));
83+
84+
System.out.print("\nCompairing character sequence of fourthString ("+fourthString+") to the character sequence of firstString ("+firstString+") returns: ");
85+
System.out.println(fourthString.compareTo(firstString));
86+
}
87+
}
88+
```
89+
View CompareLexicographically.java as GitHub Gist.
90+
91+
Output :
92+
93+
```java
94+
Comparing two strings lexicographically.
95+
96+
Compairing character sequence of the firstString (Paneer) to the character sequence of the secondString (Paneer) returns: 0
97+
98+
Compairing character sequence of secondString (Paneer) to the character sequence of thirdString (Butter) returns: 14
99+
100+
Compairing character sequence of thirdString (Butter) to the character sequence of fourthString (Cheese) returns: -1
101+
102+
Compairing character sequence of fourthString (Cheese) to the character sequence of firstString (Paneer) returns: -13
103+
```
104+
Explanation:
105+
1. In first case, compareTo() method returns zero since firstString and secondString are same.
106+
2. In second case, compareTo() method returns 14 since secondString follows thirdString by 14 characters. The pictorial explanation for this case is given below.
107+
108+
![An explanation for the second case of the java program for `compareTo()` method](/assets/images/2019-11-30/comapreto-method-secondstring-compareto-thirdstring.png)
109+
110+
3. In third case, compareTo() method returns -1 since thirdString precedes fourthString by 1 character.
111+
4. In last case, compareTo() method returns -13 since fourthString precedes firstString by 13 characters. The pictorial explanation for this case is given below.
112+
113+
![An explanation for the fourth case of the java program for `compareTo()` method](/assets/images/2019-11-30/comapreto-method-fourthString-compareto-firstString.png)
114+
115+
## 2. By creating a user-defined method
116+
117+
You can create a method that will compare two strings lexicographically.
118+
119+
First, we will see the logic, how can we build the logic for our user-defined method. I have given the step by step logic below.
120+
121+
1. Get the length of the shorter string in an integer variable lim.
122+
123+
2. Apply while loop for condition `k`<`lim` where k is integer variable initiated with 0.
124+
125+
3. Apply if condition to check if the character at an index k of both the strings is not similar; if the condition returns the difference between these two characters.
126+
127+
We will cast the difference as integer value so that the difference between the Unicode value of character will be return.
128+
129+
4. If the if condition is false, the while loop will continue for the rest of the iterations until condition is true i.e `k`<`lim`.
130+
131+
5. If the if condition is false for all iterations, return the difference between two strings.
132+
133+
I have given the program with a user-defined method `comapreString` below
134+
```java
135+
/**
136+
* The Java program to compare two strings lexicographically
137+
* by creating user defined function.
138+
*
139+
* @author Gaurav Kukade at coderolls.com
140+
*/
141+
public class CompareLexicographicallyWithUserDefinedFunction {
142+
public static void main(String[] args) {
143+
144+
String firstString = "Paneer";
145+
String secondString = "Paneer";
146+
String thirdString = "Butter";
147+
String fourthString = "Cheese";
148+
String fifthString = "PaneerButter";
149+
150+
System.out.println("Comparing two strings lexicographically by user defined function");
151+
152+
System.out.print("\nCompairing firstString ("+firstString+") to the secondString ("+secondString+") returns: ");
153+
System.out.println(compareString(firstString, secondString));
154+
155+
System.out.print("\nCompairing secondString ("+secondString+") to the thirdString ("+thirdString+") returns: ");
156+
System.out.println(compareString(secondString, thirdString));
157+
158+
System.out.print("\nCompairing thirdString ("+thirdString+") to the fourthString ("+fourthString+") returns: ");
159+
System.out.println(compareString(thirdString, fourthString));
160+
161+
System.out.print("\nCompairing fourthString ("+fourthString+") to the firstString ("+firstString+") returns: ");
162+
System.out.println(compareString(fourthString, firstString));
163+
164+
// Edge case comparing Paneer & PaneerButter
165+
System.out.print("\nCompairing firstString ("+firstString+") to the fifthString ("+fifthString+") returns: ");
166+
System.out.println(compareString(firstString, fifthString));
167+
}
168+
169+
/*
170+
* User defined function to compare two string lexicographically
171+
*/
172+
173+
public static int compareString(String str, String argumentString) {
174+
175+
int lim= Math.min(str.length(), argumentString.length());
176+
177+
int k=0;
178+
while(k<lim) {
179+
if(str.charAt(k)!= argumentString.charAt(k)) {
180+
return (int) str.charAt(k)- argumentString.charAt(k);
181+
}
182+
k++;
183+
}
184+
return str.length() - argumentString.length();
185+
}
186+
}
187+
```
188+
Output:
189+
```java
190+
Comparing two strings lexicographically by user defined function
191+
192+
Compairing firstString (Paneer) to the secondString (Paneer) returns: 0
193+
194+
Compairing secondString (Paneer) to the thirdString (Butter) returns: 14
195+
196+
Compairing thirdString (Butter) to the fourthString (Cheese) returns: -1
197+
198+
Compairing fourthString (Cheese) to the firstString (Paneer) returns: -13
199+
200+
Compairing firstString (Paneer) to the fifthString (PaneerButter) returns: -6
201+
```
202+
## Conclusion
203+
We have seen how to compare two strings lexicographically in Java. As per the articles, there are two ways to do the same
204+
1. Using the Java compareTo() method
205+
2. By creating a user-defined method
206+
207+
In a first way, I am using the compareTo() method of the Java and in the second way I have created the user-defined method compareToString().
208+
209+
Most noteworthy, In compareTo() method of the Java, the comparison is based on the Unicode value of each character in the strings.
210+
211+
If you found this article worth, please [Give me a cup of Coffee ☕](https://www.paypal.me/GauravKukade)
212+
213+
Have you tried the `compareTo()` method or created a user-defined method to compare two string lexicographically? How was your experience? Have you faced any problem? please write down the same in the comment section below.
21.4 KB
Loading
86.3 KB
Loading
Loading
Loading

assets/images/favicon.png

19.6 KB
Loading

0 commit comments

Comments
 (0)