From 10cfeea30cc558478b1031d7ba820bffb3be10d9 Mon Sep 17 00:00:00 2001 From: disha872 Date: Tue, 7 Oct 2025 16:56:34 +0530 Subject: [PATCH 1/9] Added new file for pull request --- note.txt | Bin 0 -> 48 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 note.txt diff --git a/note.txt b/note.txt new file mode 100644 index 0000000000000000000000000000000000000000..e3c2db14176b80e41fefb640ba0b89ca9bc3e807 GIT binary patch literal 48 wcmezWPoF`bL4m=MAq5Cifmned4@j2->0*X_h8%`ch7yKMAS;i7mw}4`01Gh*YybcN literal 0 HcmV?d00001 From 75e78c943edfad1334a9e44da67be9d9ceb07c7e Mon Sep 17 00:00:00 2001 From: disha872 Date: Wed, 8 Oct 2025 14:34:02 +0530 Subject: [PATCH 2/9] local --- Main.java | 98 ++++++++++++++++++ Mystring.java | 281 +++++++++++++++++++------------------------------- 2 files changed, 203 insertions(+), 176 deletions(-) create mode 100644 Main.java diff --git a/Main.java b/Main.java new file mode 100644 index 0000000..5c265f2 --- /dev/null +++ b/Main.java @@ -0,0 +1,98 @@ +/** + * @Filename- Main.java + * @Desciption- For Taking user input and calling String functions + * @Author-Disha Toshniwal + */ +import java.util.*; + +public class Main { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + Mystring mystr = new Mystring(); + + + + // Calling your methods through the object + + System.out.println("Enter a string :"); + String str=sc.nextLine(); + + System.out.println("Choose a operation :"); + System.out.println("1. Append "); + System.out.println("2. Count Words "); + System.out.println("3. Replace "); + System.out.println("4. isPalindrome "); + System.out.println("5. Splice "); + System.out.println("6. Split "); + System.out.println("7. maxrepeat "); + System.out.println("8. Sort "); + System.out.println("9. Shift "); + System.out.println("10. Reverse "); + System.out.println("11. Exit "); + System.out.println("Enter the operation you want to choose from 1 to 11:"); + int choice=sc.nextInt(); + sc.nextLine(); + switch(choice){ + case 1: + System.out.println("Enter a string to append:"); + String str1=sc.nextLine(); + mystr.append(str,str1); + break; + + case 2: + System.out.println("Enter String to count total number of words:"); + String str2=sc.nextLine(); + mystr.countWords(str2); + break; + + case 3: + System.out.println("Enter character you want to replace:"); + char a = sc.next().charAt(0); + System.out.println("Enter new character:"); + char b = sc.next().charAt(0); + mystr.replace(str,a,b); + break; + + case 4: + mystr.isPalindrome(str); + break; + + case 5: + System.out.println("Enter starting index"); + int x=sc.nextInt(); + System.out.println("Enter length"); + int y=sc.nextInt(); + mystr.splice(str,x,y); + break; + + case 6: + System.out.println("Enter pattern:"); + String pattern = sc.nextLine(); + mystr.split(str, pattern); + break; + + case 7: + mystr.maxrepeat(str); + break; + + case 8: + mystr.Sort(str); + break; + + case 9: + System.out.println("Enter shift count:"); + int n = sc.nextInt(); + mystr.Shift(str,n); + break; + + case 10: + mystr.Reverse(str); + break; + + case 11: + break; + + +} + } + }; \ No newline at end of file diff --git a/Mystring.java b/Mystring.java index 4b25c1d..25d8c82 100644 --- a/Mystring.java +++ b/Mystring.java @@ -1,208 +1,137 @@ +/** + * @Filename-Mystring.java + * @Desciption-Definition of all string functions + * @Author-Disha Toshniwal + */ import java.util.*; -class Mystring{ - public static void append(String str1,String str2){ - System.out.println("After append :\n "+str1+str2); + +public class Mystring { // class name should match file name (Mystring.java) + + public void append(String str1, String str2) { + System.out.println("After append :\n " + str1 + str2); } - public static void countWords(String str2){ - int cnt=0; - for(char c:str2.toCharArray()){ - if(c==' '){ + + public void countWords(String str2) { + int cnt = 0; + for (char c : str2.toCharArray()) { + if (c == ' ') { cnt++; } } - System.out.println("Total number of words are:"+(cnt+1)); + System.out.println("Total number of words are: " + (cnt + 1)); } - public static void replace(String str, char oldChar, char newChar) { - String result = ""; - for (int i = 0; i < str.length(); i++) { - char c = str.charAt(i); - if (c == oldChar) { - result += newChar; // replace manually - } else { - result += c; // keep original + public void replace(String str, char oldChar, char newChar) { + String result = ""; + for (int i = 0; i < str.length(); i++) { + char c = str.charAt(i); + if (c == oldChar) { + result += newChar; + } else { + result += c; + } } + System.out.println("String after Replace: " + result); } - System.out.println("String after Replace: " + result); -} - - public static void isPalindrome(String str){ - int i=0; - int j=str.length()-1; - while(i words = new ArrayList<>(); + String temp = ""; + + for (int i = 0; i < str.length(); ) { + // if the pattern matches starting at position i + if (i + pattern.length() <= str.length() && + str.substring(i, i + pattern.length()).equals(pattern)) { + words.add(temp); + temp = ""; + i += pattern.length(); // skip the pattern } else { - words[index++] = word; - word = ""; + temp += str.charAt(i); + i++; } } - words[index] = word; // last word - System.out.println(Arrays.toString(words)); -} - public static void maxrepeat(String str) { - int[] freq = new int[256]; // for ASCII characters + // add the last word + if (!temp.isEmpty()) { + words.add(temp); + } - // count frequency of each character - for (int i = 0; i < str.length(); i++) { - char c = str.charAt(i); - freq[c]++; + // print the result + System.out.println("Result after splitting:"); + for (String word : words) { + System.out.print(word + "\t"); } +} - int max = -1; - char result = ' '; - // find the character with maximum frequency - for (int i = 0; i < str.length(); i++) { - if (max < freq[str.charAt(i)]) { - max = freq[str.charAt(i)]; - result = str.charAt(i); + + public void maxrepeat(String str) { + int[] freq = new int[256]; + for (int i = 0; i < str.length(); i++) { + char c = str.charAt(i); + freq[c]++; } - } - System.out.println("Character with maximum frequency: " + result); - System.out.println("Frequency: " + max); -} -public static void Sort(String str){ - - char[] arr = str.toCharArray(); // convert string to char array - - // Bubble Sort on characters - for (int i = 0; i < arr.length - 1; i++) { - for (int j = 0; j < arr.length - i - 1; j++) { - if (arr[j] > arr[j + 1]) { - char temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; + int max = -1; + char result = ' '; + for (int i = 0; i < str.length(); i++) { + if (max < freq[str.charAt(i)]) { + max = freq[str.charAt(i)]; + result = str.charAt(i); } } - } - - String sortedStr = new String(arr); - System.out.println("Sorted string: " + sortedStr); -} -public static void Shift(String str,int n){ - n = n % str.length(); // handle if n > length - - // move first n chars to end - String shifted = str.substring(str.length() - n) + str.substring(0, str.length() - n); - System.out.println("Shifted string: " + shifted); -} -public static void Reverse(String str){ - String word=""; - for(int i=str.length()-1;i>=0;i--){ - word+=str.charAt(i); + System.out.println("Character with maximum frequency: " + result); + System.out.println("Frequency: " + max); } - System.out.println("Reversed String is :"+word); -} + public void Sort(String str) { + char[] arr = str.toCharArray(); + for (int i = 0; i < arr.length - 1; i++) { + for (int j = 0; j < arr.length - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + char temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + String sortedStr = new String(arr); + System.out.println("Sorted string: " + sortedStr); + } - public static void main(String[] args){ - Scanner sc=new Scanner(System.in); - System.out.println("Enter a string :"); - String str=sc.nextLine(); - System.out.println("Choose a operation :"); - System.out.println("1. Append "); - System.out.println("2. Count Words "); - System.out.println("3. Replace "); - System.out.println("4. isPalindrome "); - System.out.println("5. Splice "); - System.out.println("6. Split "); - System.out.println("7. maxrepeat "); - System.out.println("8. Sort "); - System.out.println("9. Shift "); - System.out.println("10. Reverse "); - System.out.println("11. Exit "); - System.out.println("Enter the operation you want to choose from 1 to 11:"); - int choice=sc.nextInt(); - sc.nextLine(); - switch(choice){ - case 1: - System.out.println("Enter a string to append:"); - String str1=sc.nextLine(); - append(str,str1); - break; - - case 2: - System.out.println("Enter String to count total number of words:"); - String str2=sc.nextLine(); - countWords(str2); - break; - - case 3: - System.out.println("Enter character you want to replace:"); - char a = sc.next().charAt(0); - System.out.println("Enter new character:"); - char b = sc.next().charAt(0); - replace(str,a,b); - break; - - case 4: - isPalindrome(str); - break; - - case 5: - splice(str,0,2); - break; - - case 6: - System.out.println("Enter string you want to split "); - String s=sc.nextLine(); - split(s); - break; - - case 7: - maxrepeat(str); - break; - - case 8: - Sort(str); - break; - - case 9: - System.out.println("Enter shift count:"); - int n = sc.nextInt(); - Shift(str,n); - break; - - case 10: - Reverse(str); - break; - - case 11: - break; - + public void Shift(String str, int n) { + n = n % str.length(); + String shifted = str.substring(str.length() - n) + str.substring(0, str.length() - n); + System.out.println("Shifted string: " + shifted); } + + public void Reverse(String str) { + String word = ""; + for (int i = str.length() - 1; i >= 0; i--) { + word += str.charAt(i); + } + System.out.println("Reversed String is: " + word); } -}; \ No newline at end of file +} From e9bc8263cdca3243352725e7852b65cf7cd8f96e Mon Sep 17 00:00:00 2001 From: disha872 Date: Wed, 8 Oct 2025 20:24:00 +0530 Subject: [PATCH 3/9] changes --- .idea/workspace.xml | 74 ++++++++++++++++ Main.class | Bin 0 -> 2402 bytes Main.java | 191 +++++++++++++++++++++-------------------- Mystring.class | Bin 3648 -> 3656 bytes Mystring.java | 202 ++++++++++++++++++++++---------------------- note.txt | Bin 48 -> 0 bytes 6 files changed, 274 insertions(+), 193 deletions(-) create mode 100644 .idea/workspace.xml create mode 100644 Main.class delete mode 100644 note.txt diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..696e6e6 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,74 @@ + + + + + + + + + + + + + + + { + "associatedIndex": 1 +} + + + + { + "keyToString": { + "Application.Main.executor": "Run", + "ModuleVcsDetector.initialDetectionPerformed": "true", + "RunOnceActivity.ShowReadmeOnStart": "true", + "RunOnceActivity.git.unshallow": "true", + "git-widget-placeholder": "my-branch", + "ignore.virus.scanning.warn.message": "true", + "kotlin-language-version-configured": "true", + "last_opened_file_path": "C:/Users/Disha Toshniwal/OneDrive/Desktop/Java-Code", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "vue.rearranger.settings.migration": "true" + } +} + + + + + + + + + + 1759909425714 + + + + + + \ No newline at end of file diff --git a/Main.class b/Main.class new file mode 100644 index 0000000000000000000000000000000000000000..98bc24781f103f15130ae2c66a5b7cdbe4a381e7 GIT binary patch literal 2402 zcmZuyOHdqD6g>|HdS>W^gaiorHi-$4z>vv@5Xc|Fj6y;(3FK#DGc+({=pKjWr%@A) znwa>-Rd!ldO=-C?WeruPN*8gJl}pQo3#%;M=}IljGM?Mrl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z(AT1g=fXlyxUs2Hs|*)#F^}0s?sXuKTM}jYQAb)L(i@)dMHwHdj24j2Bjs&KhR6Kj z+O{%sDl5Rrqf5YYc^nWskw=TfP0z#VgwYFlE|2vx{$d_gGJcKYd6dcLD}Ly!Qt2YE z(tqLrzsvZqV+G2w2FqaZoYb?o9V^j_N*qEJj`B(F#VVXZ4KJkC7-3|VkL@%ibd`_o z4bFOtGMnYI`3*MXTWrDisKbv$`Wf5sD;n_|n(!B5c!;>tutP1!PF2H)YZG>>Mm|=} z*sFH&UD|{F>L8z=P8?J{IHZy|tS;b)y2M9i5M3(6mn4UyY6b~)1>Ncz@BZuPRX1@$ z-Nq^PK2EDU=u;n{Uwwpg>SNx-pW>qW9GBD_2Gm!u)O`$*FR&3y9+Gg0hPO3n8Y(nS X0;;Q;wYs&MR;@)fD(YP?U+?_`^^i6i literal 0 HcmV?d00001 diff --git a/Main.java b/Main.java index 5c265f2..3fb27b9 100644 --- a/Main.java +++ b/Main.java @@ -1,98 +1,103 @@ /** - * @Filename- Main.java - * @Desciption- For Taking user input and calling String functions - * @Author-Disha Toshniwal - */ -import java.util.*; + +* @Filename- Main.java +* @Description- For taking user input and calling string functions +* @Author- Disha Toshniwal + */ + import java.util.*; public class Main { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - Mystring mystr = new Mystring(); - - - - // Calling your methods through the object - - System.out.println("Enter a string :"); - String str=sc.nextLine(); - - System.out.println("Choose a operation :"); - System.out.println("1. Append "); - System.out.println("2. Count Words "); - System.out.println("3. Replace "); - System.out.println("4. isPalindrome "); - System.out.println("5. Splice "); - System.out.println("6. Split "); - System.out.println("7. maxrepeat "); - System.out.println("8. Sort "); - System.out.println("9. Shift "); - System.out.println("10. Reverse "); - System.out.println("11. Exit "); - System.out.println("Enter the operation you want to choose from 1 to 11:"); - int choice=sc.nextInt(); - sc.nextLine(); - switch(choice){ - case 1: - System.out.println("Enter a string to append:"); - String str1=sc.nextLine(); - mystr.append(str,str1); - break; - - case 2: - System.out.println("Enter String to count total number of words:"); - String str2=sc.nextLine(); - mystr.countWords(str2); - break; - - case 3: - System.out.println("Enter character you want to replace:"); - char a = sc.next().charAt(0); - System.out.println("Enter new character:"); - char b = sc.next().charAt(0); - mystr.replace(str,a,b); - break; - - case 4: - mystr.isPalindrome(str); - break; - - case 5: - System.out.println("Enter starting index"); - int x=sc.nextInt(); - System.out.println("Enter length"); - int y=sc.nextInt(); - mystr.splice(str,x,y); - break; - - case 6: - System.out.println("Enter pattern:"); - String pattern = sc.nextLine(); - mystr.split(str, pattern); +public static void main(String[] args) { +Scanner scanner = new Scanner(System.in); +Mystring myStringFunctions = new Mystring(); + +System.out.println("Enter a string : "); +String mainString = scanner.nextLine(); +System.out.println("Choose an operation : "); +System.out.println("1. Append "); +System.out.println("2. Count Words "); +System.out.println("3. Replace "); +System.out.println("4. isPalindrome "); +System.out.println("5. Splice "); +System.out.println("6. Split "); +System.out.println("7. Max Repeat "); +System.out.println("8. Sort "); +System.out.println("9. Shift "); +System.out.println("10. Reverse "); +System.out.println("11. Exit "); + +System.out.print("Enter your choice (1 – 11) : "); +int userChoice = scanner.nextInt(); +scanner.nextLine(); // clear buffer + +switch (userChoice) { + case 1: + System.out.println("Enter a string to append : "); + String appendString = scanner.nextLine(); + myStringFunctions.append(mainString, appendString); + break; + + case 2: + System.out.println("Enter string to count total number of words : "); + String wordCountString = scanner.nextLine(); + myStringFunctions.countWords(wordCountString); + break; + + case 3: + System.out.println("Enter character you want to replace : "); + char oldCharacter = scanner.next().charAt(0); + System.out.println("Enter new character : "); + char newCharacter = scanner.next().charAt(0); + myStringFunctions.replace(mainString, oldCharacter, newCharacter); + break; + + case 4: + myStringFunctions.isPalindrome(mainString); + break; + + case 5: + System.out.println("Enter starting index : "); + int startIndex = scanner.nextInt(); + System.out.println("Enter length to remove : "); + int removeLength = scanner.nextInt(); + myStringFunctions.splice(mainString, startIndex, removeLength); + break; + + case 6: + scanner.nextLine(); // clear buffer + System.out.println("Enter pattern to split by : "); + String splitPattern = scanner.nextLine(); + myStringFunctions.split(mainString, splitPattern); + break; + + case 7: + myStringFunctions.maxrepeat(mainString); + break; + + case 8: + myStringFunctions.sort(mainString); + break; + + case 9: + System.out.println("Enter shift count : "); + int shiftCount = scanner.nextInt(); + myStringFunctions.shift(mainString, shiftCount); + break; + + case 10: + myStringFunctions.reverse(mainString); break; - - case 7: - mystr.maxrepeat(str); - break; - - case 8: - mystr.Sort(str); - break; - - case 9: - System.out.println("Enter shift count:"); - int n = sc.nextInt(); - mystr.Shift(str,n); - break; - - case 10: - mystr.Reverse(str); - break; - - case 11: - break; - - + + case 11: + System.out.println("Exiting... "); + break; + + default: + System.out.println("Invalid choice! Please enter a number between 1 and 11. "); +} + +scanner.close(); +} + + } - } - }; \ No newline at end of file diff --git a/Mystring.class b/Mystring.class index 6cef4305b4e346feb2ff5c1c7c465bbcd95716c2..944a2b0b0adb3804a5de3676bd2e8f1494c81d07 100644 GIT binary patch literal 3656 zcmaJ^TXPi075-W~yR%vbAuX(s@CpWTlUQARk!6rTHms2?M-mr-xX5-i+8v=mtC?kI zmP=ydIF@5O_j`;Vka10+NP=kmL%|H|}f%;3<6|1{oxfi>SPhGNefk5n#<2u2BK%}jG zycTiP>PQ&a0AtDS~~8<{zqrpADv1diOo8;7)YT}U~?4+0(wybD!9a0 z<(&5M1e(ySW1E4;kX|35f+DaX@Uk;j=}4($T^G>W+9$F}2yB;u?T~JVs$h#_rvRk$ zT?Tez4|y%v?!{n6@j4_I_8DkHI~Q`&J4w{&iLPsqk*+%g_EkZgt#Vir5p+qLZh^g3 zY^s>V@r1yZp}=%})Aa(gXcZhcU-IVcIGz$nhAY;Zc?tPx1HE`gpw=%>`4xH^+lHzU zAh#cs+jq!_>XOjVrQ`5Qn#zGw=vI^tJARPBAw;Cba|Vv!C^@k&m#qTDu`X$$MElz> z$oA)_TezD*L0aex~ z-HaF*#c_eS!!;}LN&<~-6~YwAp~`&!2Asqx9b*Pg%X5-SZtJ%2@EOY=wXX$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5nfK#?LhTT<)xIvNY7Tq`p#Vd%>BrI=q11N9{Dr#mTXPUfZm# z@95=m#yEsI}{657`G5!5@IPRme>Eq32K9v*9dN7h7yJ9A!v-F zah?cjIETS+`CdLZbI!vk!z=8qMqph+(6CEr_=aE}vHvPWT<$)iR~bPfdbH9QQBa6$ z&uz5CW5E?PN<6i|h2l zH1VmEw!=KkBv7YNRv~y7VTthro=M(E8SwxSbj54yF0Y_x4ZY;YW25gL8(syKDfk`a;Q*)a zWHjz-ezf=4CzZS!=novN^T@AOS>3CH64t@LSp>hsu3=B5fYinF?TE)hQmAk&Tw&Zq zd0Q;JAypYGiXycoIcO=wAb+BvElG^6E9jTS>hW?K+xdKuZ(Y!teGS`s^q>((SWdlU zqn~d_aRSHi7>^-I3}6B$af$XRp4rp1W^qPw)&%V?f8jydQeKwd2iQx6 zqn^Uw#&|@){>pZU;yk6qjj+tl@@#vKRsIAk`aFv+N&QBt-vz2P z#-4FJ$=_vM#4Mhob`w~{(|8fj@XLM)FXLIf1rw{7#2uvYd#d~g%;3-T{wvk~8&6Vy z$2|VYee^$Y@E)?%_Npl5Nl8aRRA50=@vKsV=R_Tz7Y#go#PNdYpx$y@s)Y6--qLVf z7DaWeJsgq$4_;$W3BHT(u{F2@{FKapU)_JG?l;x_$LjtQ{DNLldi)Z splitParts = new ArrayList<>(); + String currentSegment = ""; - -public void split(String str, String pattern) { - ArrayList words = new ArrayList<>(); - String temp = ""; - - for (int i = 0; i < str.length(); ) { - // if the pattern matches starting at position i - if (i + pattern.length() <= str.length() && - str.substring(i, i + pattern.length()).equals(pattern)) { - words.add(temp); - temp = ""; - i += pattern.length(); // skip the pattern + for (int index = 0; index < inputString.length();) { + if (index + delimiter.length() <= inputString.length() && + inputString.substring(index, index + delimiter.length()).equals(delimiter)) { + splitParts.add(currentSegment); + currentSegment = ""; + index += delimiter.length(); } else { - temp += str.charAt(i); - i++; + currentSegment += inputString.charAt(index); + index++; } } - // add the last word - if (!temp.isEmpty()) { - words.add(temp); + if (!currentSegment.isEmpty()) { + splitParts.add(currentSegment); } - // print the result System.out.println("Result after splitting:"); - for (String word : words) { - System.out.print(word + "\t"); + for (String segment : splitParts) { + System.out.print(segment + "\t"); } } +public void maxrepeat(String inputString) { + int[] frequency = new int[256]; + for (int index = 0; index < inputString.length(); index++) { + char currentChar = inputString.charAt(index); + frequency[currentChar]++; + } + int maxFrequency = -1; + char mostFrequentChar = ' '; - public void maxrepeat(String str) { - int[] freq = new int[256]; - for (int i = 0; i < str.length(); i++) { - char c = str.charAt(i); - freq[c]++; - } - - int max = -1; - char result = ' '; - for (int i = 0; i < str.length(); i++) { - if (max < freq[str.charAt(i)]) { - max = freq[str.charAt(i)]; - result = str.charAt(i); - } + for (int index = 0; index < inputString.length(); index++) { + char currentChar = inputString.charAt(index); + if (maxFrequency < frequency[currentChar]) { + maxFrequency = frequency[currentChar]; + mostFrequentChar = currentChar; } - - System.out.println("Character with maximum frequency: " + result); - System.out.println("Frequency: " + max); } - public void Sort(String str) { - char[] arr = str.toCharArray(); - for (int i = 0; i < arr.length - 1; i++) { - for (int j = 0; j < arr.length - i - 1; j++) { - if (arr[j] > arr[j + 1]) { - char temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } + System.out.println("Character with maximum frequency: " + mostFrequentChar); + System.out.println("Frequency: " + maxFrequency); +} + +public void sort(String inputString) { + char[] charArray = inputString.toCharArray(); + for (int i = 0; i < charArray.length - 1; i++) { + for (int j = 0; j < charArray.length - i - 1; j++) { + if (charArray[j] > charArray[j + 1]) { + char tempChar = charArray[j]; + charArray[j] = charArray[j + 1]; + charArray[j + 1] = tempChar; } } - String sortedStr = new String(arr); - System.out.println("Sorted string: " + sortedStr); } + String sortedString = new String(charArray); + System.out.println("Sorted string: " + sortedString); +} - public void Shift(String str, int n) { - n = n % str.length(); - String shifted = str.substring(str.length() - n) + str.substring(0, str.length() - n); - System.out.println("Shifted string: " + shifted); - } +public void shift(String inputString, int shiftCount) { + shiftCount = shiftCount % inputString.length(); + String shiftedString = inputString.substring(inputString.length() - shiftCount) + + inputString.substring(0, inputString.length() - shiftCount); + System.out.println("Shifted string: " + shiftedString); +} - public void Reverse(String str) { - String word = ""; - for (int i = str.length() - 1; i >= 0; i--) { - word += str.charAt(i); - } - System.out.println("Reversed String is: " + word); +public void reverse(String inputString) { + String reversedString = ""; + for (int index = inputString.length() - 1; index >= 0; index--) { + reversedString += inputString.charAt(index); } + System.out.println("Reversed String is: " + reversedString); +} + + } diff --git a/note.txt b/note.txt deleted file mode 100644 index e3c2db14176b80e41fefb640ba0b89ca9bc3e807..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 48 wcmezWPoF`bL4m=MAq5Cifmned4@j2->0*X_h8%`ch7yKMAS;i7mw}4`01Gh*YybcN From ec1ce5d64ee9f2d4ad4e79bc1d657cfb0d0de94f Mon Sep 17 00:00:00 2001 From: disha872 Date: Fri, 10 Oct 2025 13:37:09 +0530 Subject: [PATCH 4/9] countWords --- Mystring.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Mystring.java b/Mystring.java index 078d9ca..c3275f7 100644 --- a/Mystring.java +++ b/Mystring.java @@ -15,8 +15,8 @@ public void append(String firstString, String secondString) { public void countWords(String inputString) { int wordCount = 0; - for (char currentChar : inputString.toCharArray()) { - if (currentChar == ' ') { + for (int i=0;i Date: Fri, 10 Oct 2025 14:29:13 +0530 Subject: [PATCH 5/9] comments --- Mystring.java | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Mystring.java b/Mystring.java index c3275f7..618fb0e 100644 --- a/Mystring.java +++ b/Mystring.java @@ -8,11 +8,11 @@ public class Mystring { - +//ADD TWO STRING public void append(String firstString, String secondString) { System.out.println("After append :\n " + firstString + secondString); } - +// Count Total Number Of Words public void countWords(String inputString) { int wordCount = 0; for (int i=0;i splitParts = new ArrayList<>(); String currentSegment = ""; @@ -82,7 +82,7 @@ public void split(String inputString, String delimiter) { System.out.print(segment + "\t"); } } - +// print the maximum repeating character in a string public void maxrepeat(String inputString) { int[] frequency = new int[256]; for (int index = 0; index < inputString.length(); index++) { @@ -104,7 +104,7 @@ public void maxrepeat(String inputString) { System.out.println("Character with maximum frequency: " + mostFrequentChar); System.out.println("Frequency: " + maxFrequency); } - +// sort the input string public void sort(String inputString) { char[] charArray = inputString.toCharArray(); for (int i = 0; i < charArray.length - 1; i++) { @@ -119,14 +119,14 @@ public void sort(String inputString) { String sortedString = new String(charArray); System.out.println("Sorted string: " + sortedString); } - +// rotate the string public void shift(String inputString, int shiftCount) { shiftCount = shiftCount % inputString.length(); String shiftedString = inputString.substring(inputString.length() - shiftCount) + inputString.substring(0, inputString.length() - shiftCount); System.out.println("Shifted string: " + shiftedString); } - +//reverse the string public void reverse(String inputString) { String reversedString = ""; for (int index = inputString.length() - 1; index >= 0; index--) { From 524b94652bfe45cee489e626a29cf406d46af3f9 Mon Sep 17 00:00:00 2001 From: disha872 Date: Sat, 11 Oct 2025 22:38:03 +0530 Subject: [PATCH 6/9] second assignment --- MyOperation.java | 223 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 223 insertions(+) create mode 100644 MyOperation.java diff --git a/MyOperation.java b/MyOperation.java new file mode 100644 index 0000000..9334492 --- /dev/null +++ b/MyOperation.java @@ -0,0 +1,223 @@ +public class MyOperation { + + // 1. Count Unique Palindromes (Length >= 3) + public int countUniquePalindromes(String inputString) { + if (inputString == null || !inputString.matches("[a-zA-Z]+")) { + System.out.println("Invalid input! Please enter a valid alphabetic string."); + return 0; + } + + int palindromeCount = 0; + for (int startIndex = 0; startIndex < inputString.length(); startIndex++) { + for (int endIndex = startIndex + 2; endIndex < inputString.length(); endIndex++) { + if (isPalindrome(inputString, startIndex, endIndex)) { + palindromeCount++; + } + } + } + return palindromeCount; + } + + private boolean isPalindrome(String text, int startIndex, int endIndex) { + while (startIndex < endIndex) { + if (text.charAt(startIndex) != text.charAt(endIndex)) return false; + startIndex++; + endIndex--; + } + return true; + } + + // 2. Nth Fibonacci (Recursive) + public int nthFibonacci(int n) { + if (n < 0) { + System.out.println("Invalid input! Please enter a non-negative integer."); + return -1; + } + if (n <= 1) return n; + return nthFibonacci(n - 1) + nthFibonacci(n - 2); + } + + // 3. Snake to Camel Case + public String snakeToCamel(String snakeString) { + if (snakeString == null || !snakeString.matches("[a-z_]+")) { + System.out.println("Invalid input! Please enter a valid lowercase snake_case string."); + return ""; + } + + StringBuilder camelCaseResult = new StringBuilder(); + boolean convertNext = false; + + for (int index = 0; index < snakeString.length(); index++) { + char currentChar = snakeString.charAt(index); + if (currentChar == '_') { + convertNext = true; + } else { + if (convertNext && Character.isLowerCase(currentChar)) { + camelCaseResult.append(Character.toUpperCase(currentChar)); + } else { + camelCaseResult.append(currentChar); + } + convertNext = false; + } + } + return camelCaseResult.toString(); + } + + // 4. Count Consonants + public int countConsonants(String inputText) { + if (inputText == null || !inputText.matches("[a-zA-Z]+")) { + System.out.println("Invalid input! Please enter a valid alphabetic string."); + return 0; + } + + int consonantCount = 0; + for (int index = 0; index < inputText.length(); index++) { + char ch = Character.toLowerCase(inputText.charAt(index)); + if (ch >= 'a' && ch <= 'z' && "aeiou".indexOf(ch) == -1) { + consonantCount++; + } + } + return consonantCount; + } + + // 5. Binary to Decimal + public int binaryToDecimal(String binaryString) { + if (binaryString == null || !binaryString.matches("[01]+")) { + System.out.println("Invalid input! Please enter a valid binary string (only 0s and 1s)."); + return -1; + } + + int decimalValue = 0; + int base = 1; + for (int index = binaryString.length() - 1; index >= 0; index--) { + if (binaryString.charAt(index) == '1') { + decimalValue += base; + } + base *= 2; + } + return decimalValue; + } + + // 6. Display Characters (e.g., "a1b3" → "abbb") + public void displayCharacters(String inputString) { + if (inputString == null || !inputString.matches("([a-zA-Z][0-9])+")) { + System.out.println("Invalid input! Please enter a valid pattern like 'a1b2c3'."); + return; + } + + StringBuilder expandedString = new StringBuilder(); + for (int index = 0; index < inputString.length() - 1; index++) { + char currentChar = inputString.charAt(index); + char nextChar = inputString.charAt(index + 1); + if (Character.isLetter(currentChar) && Character.isDigit(nextChar)) { + int repeatCount = nextChar - '0'; + expandedString.append(String.valueOf(currentChar).repeat(repeatCount)); + } + } + System.out.println("Expanded String: " + expandedString); + } + + // 7. Character Frequency Compression (e.g., "aaabb" → "a3b2") + public String charFrequencyCompressed(String inputString) { + if (inputString == null || !inputString.matches("[a-zA-Z]+")) { + System.out.println("Invalid input! Please enter a valid alphabetic string."); + return ""; + } + + StringBuilder compressedString = new StringBuilder(); + int index = 0; + + while (index < inputString.length()) { + char currentChar = inputString.charAt(index); + int count = 1; + int nextIndex = index + 1; + while (nextIndex < inputString.length() && inputString.charAt(nextIndex) == currentChar) { + count++; + nextIndex++; + } + compressedString.append(currentChar).append(count); + index = nextIndex; + } + + return compressedString.toString(); + } + + // 8. Check Prime Number + public boolean isPrime(int number) { + if (number <= 1) { + System.out.println("Invalid input! Please enter a number greater than 1."); + return false; + } + for (int divisor = 2; divisor * divisor <= number; divisor++) { + if (number % divisor == 0) return false; + } + return true; + } + + // 9. Number to Words (0–9999) + public String numberToWords(int number) { + if (number < 0 || number > 9999) { + System.out.println("Invalid input! Please enter a number between 0 and 9999."); + return ""; + } + + if (number == 0) return "zero"; + + String[] ones = { + "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", + "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", + "sixteen", "seventeen", "eighteen", "nineteen" + }; + String[] tens = { + "", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" + }; + + StringBuilder words = new StringBuilder(); + + if (number / 1000 > 0) { + words.append(ones[number / 1000]).append(" thousand "); + number %= 1000; + } + if (number / 100 > 0) { + words.append(ones[number / 100]).append(" hundred "); + number %= 100; + } + if (number > 0) { + if (number < 20) { + words.append(ones[number]); + } else { + words.append(tens[number / 10]); + if (number % 10 > 0) { + words.append(" ").append(ones[number % 10]); + } + } + } + return words.toString().trim(); + } + + // 10. Longest Substring Without Repeating Characters + public int longestSubstringWithoutRepeat(String inputString) { + if (inputString == null || !inputString.matches("[a-zA-Z]+")) { + System.out.println("Invalid input! Please enter a valid alphabetic string."); + return 0; + } + + int maxLength = 0; + for (int start = 0; start < inputString.length(); start++) { + boolean[] seenCharacters = new boolean[256]; + int currentLength = 0; + + for (int end = start; end < inputString.length(); end++) { + char currentChar = inputString.charAt(end); + if (seenCharacters[currentChar]) break; + seenCharacters[currentChar] = true; + currentLength++; + } + + if (currentLength > maxLength) { + maxLength = currentLength; + } + } + return maxLength; + } +} From d4b9db2772d0260966b26e354800ed13961a3db0 Mon Sep 17 00:00:00 2001 From: disha872 Date: Tue, 14 Oct 2025 20:17:52 +0530 Subject: [PATCH 7/9] Changes --- MyOperation.java | 58 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 17 deletions(-) diff --git a/MyOperation.java b/MyOperation.java index 9334492..3e4ada5 100644 --- a/MyOperation.java +++ b/MyOperation.java @@ -1,3 +1,8 @@ +/** + * @Filename - Main.java + * @Description - Definition of functions + * @Author - Disha Toshniwal + */ public class MyOperation { // 1. Count Unique Palindromes (Length >= 3) @@ -9,7 +14,7 @@ public int countUniquePalindromes(String inputString) { int palindromeCount = 0; for (int startIndex = 0; startIndex < inputString.length(); startIndex++) { - for (int endIndex = startIndex + 2; endIndex < inputString.length(); endIndex++) { + for (int endIndex = startIndex + 1; endIndex < inputString.length(); endIndex++){ if (isPalindrome(inputString, startIndex, endIndex)) { palindromeCount++; } @@ -28,13 +33,13 @@ private boolean isPalindrome(String text, int startIndex, int endIndex) { } // 2. Nth Fibonacci (Recursive) - public int nthFibonacci(int n) { - if (n < 0) { + public int nthFibonacci(int number) { + if (number < 0) { System.out.println("Invalid input! Please enter a non-negative integer."); return -1; } - if (n <= 1) return n; - return nthFibonacci(n - 1) + nthFibonacci(n - 2); + if (number <= 1) return number; + return nthFibonacci(number - 1) + nthFibonacci(number - 2); } // 3. Snake to Camel Case @@ -100,23 +105,42 @@ public int binaryToDecimal(String binaryString) { // 6. Display Characters (e.g., "a1b3" → "abbb") public void displayCharacters(String inputString) { - if (inputString == null || !inputString.matches("([a-zA-Z][0-9])+")) { - System.out.println("Invalid input! Please enter a valid pattern like 'a1b2c3'."); - return; - } + if (inputString == null || !inputString.matches("([a-zA-Z][0-9]+)+")) { + System.out.println("Invalid input! Please enter a valid pattern like 'a1b2c3'."); + return; + } - StringBuilder expandedString = new StringBuilder(); - for (int index = 0; index < inputString.length() - 1; index++) { - char currentChar = inputString.charAt(index); - char nextChar = inputString.charAt(index + 1); - if (Character.isLetter(currentChar) && Character.isDigit(nextChar)) { - int repeatCount = nextChar - '0'; - expandedString.append(String.valueOf(currentChar).repeat(repeatCount)); + StringBuilder expandedString = new StringBuilder(); + int index = 0; + + while (index < inputString.length()) { + char currentChar = inputString.charAt(index); + + // Ensure currentChar is a letter + if (Character.isLetter(currentChar)) { + index++; + StringBuilder numStr = new StringBuilder(); + + // Collect all following digits (for multi-digit numbers) + while (index < inputString.length() && Character.isDigit(inputString.charAt(index))) { + numStr.append(inputString.charAt(index)); + index++; } + + // Convert collected number string to integer + int repeatCount = Integer.parseInt(numStr.toString()); + + // Repeat the character + expandedString.append(String.valueOf(currentChar).repeat(repeatCount)); + } else { + index++; // skip any invalid character just in case } - System.out.println("Expanded String: " + expandedString); } + System.out.println("Expanded String: " + expandedString); +} + + // 7. Character Frequency Compression (e.g., "aaabb" → "a3b2") public String charFrequencyCompressed(String inputString) { if (inputString == null || !inputString.matches("[a-zA-Z]+")) { From 82378c991c2b6378565abe09aeb262b3be5000ca Mon Sep 17 00:00:00 2001 From: disha872 Date: Tue, 14 Oct 2025 20:18:54 +0530 Subject: [PATCH 8/9] Changes --- MainOperation.java | 88 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 MainOperation.java diff --git a/MainOperation.java b/MainOperation.java new file mode 100644 index 0000000..2f68b35 --- /dev/null +++ b/MainOperation.java @@ -0,0 +1,88 @@ +/** + * @Filename - Main.java + * @Description - For taking user input and calling string functions + * @Author - Disha Toshniwal + */ +import java.util.*; +public class MainOperation{ +public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + MyOperation operation = new MyOperation(); + while (true) { + System.out.println("\n========= MENU =========\n1. Count Unique Palindromes\n2. Nth Fibonacci\n3. Snake to Camel Case\n4. Count Consonants\n5. Binary to Decimal\n6. Display Characters\n7. Character Frequency Compression\n8. Check Prime Number\n9. Number to Words\n10. Longest Substring Without Repeating Characters\n0. Exit"); + System.out.print("Enter your choice: "); + int choice = -1; + try { + choice = Integer.parseInt(scanner.nextLine()); + } catch (Exception e) { + System.out.println("Invalid input! Please enter a number between 0 and 10."); + continue; + } + + if (choice == 0) { + System.out.println("Exiting program. Goodbye!"); + break; + } + + switch (choice) { + case 1: + System.out.print("Enter a string: "); + System.out.println("Unique Palindromes Count: " + operation.countUniquePalindromes(scanner.nextLine())); + break; + case 2: + System.out.print("Enter n: "); + try { + System.out.println("Fibonacci Number: " + operation.nthFibonacci(Integer.parseInt(scanner.nextLine()))); + } catch (Exception e) { + System.out.println("Invalid input! Please enter an integer."); + } + break; + case 3: + System.out.print("Enter snake_case string: "); + System.out.println("CamelCase: " + operation.snakeToCamel(scanner.nextLine())); + break; + case 4: + System.out.print("Enter a string: "); + System.out.println("Consonant Count: " + operation.countConsonants(scanner.nextLine())); + break; + case 5: + System.out.print("Enter binary string: "); + System.out.println("Decimal Value: " + operation.binaryToDecimal(scanner.nextLine())); + break; + case 6: + System.out.print("Enter pattern (like a1b2): "); + operation.displayCharacters(scanner.nextLine()); + break; + case 7: + System.out.print("Enter a string: "); + System.out.println("Compressed String: " + operation.charFrequencyCompressed(scanner.nextLine())); + break; + case 8: + System.out.print("Enter number: "); + try { + int num = Integer.parseInt(scanner.nextLine()); + System.out.println(num + (operation.isPrime(num) ? " is Prime" : " is Not Prime")); + } catch (Exception e) { + System.out.println("Invalid input! Please enter a valid integer."); + } + break; + case 9: + System.out.print("Enter a number (0-9999): "); + try { + System.out.println("In Words: " + operation.numberToWords(Integer.parseInt(scanner.nextLine()))); + } catch (Exception e) { + System.out.println("Invalid input! Please enter a valid integer between 0 and 9999."); + } + break; + case 10: + System.out.print("Enter a string: "); + System.out.println("Longest Unique Substring Length: " + operation.longestSubstringWithoutRepeat(scanner.nextLine())); + break; + default: + System.out.println("Invalid choice! Please enter between 0–10."); + } + } + + scanner.close(); + } +} \ No newline at end of file From 8d2bceded18f10ccf927730819e76ba97c78ec43 Mon Sep 17 00:00:00 2001 From: disha872 Date: Thu, 16 Oct 2025 21:03:08 +0530 Subject: [PATCH 9/9] 2048game --- 2048.cpp | 156 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 2048.cpp diff --git a/2048.cpp b/2048.cpp new file mode 100644 index 0000000..533adca --- /dev/null +++ b/2048.cpp @@ -0,0 +1,156 @@ +#include +#include +#include +#include +#include // for getch() on Windows + +using namespace std; + +const int SIZE = 4; +int grid[SIZE][SIZE]; + +// Initialize grid +void initGrid() { + for (int i = 0; i < SIZE; i++) + for (int j = 0; j < SIZE; j++) + grid[i][j] = 0; +} + +// Add random tile (2 or 4) +void addRandomTile() { + int empty[SIZE * SIZE][2]; + int count = 0; + for (int i = 0; i < SIZE; i++) + for (int j = 0; j < SIZE; j++) + if (grid[i][j] == 0) { + empty[count][0] = i; + empty[count][1] = j; + count++; + } + if (count > 0) { + int r = rand() % count; + grid[empty[r][0]][empty[r][1]] = (rand() % 2 + 1) * 2; + } +} + +// Print grid +void printGrid() { + cout << "\n"; + for (int i = 0; i < SIZE; i++) { + for (int j = 0; j < SIZE; j++) { + if (grid[i][j] == 0) + cout << setw(5) << "."; + else + cout << setw(5) << grid[i][j]; + } + cout << "\n\n"; + } +} + +// Slide left +bool slideLeft() { + bool moved = false; + for (int i = 0; i < SIZE; i++) { + int temp[SIZE] = {0}; + int idx = 0; + for (int j = 0; j < SIZE; j++) { + if (grid[i][j] != 0) { + if (temp[idx] == 0) { + temp[idx] = grid[i][j]; + } else if (temp[idx] == grid[i][j]) { + temp[idx++] *= 2; + moved = true; + } else { + temp[++idx] = grid[i][j]; + } + if (j != idx) moved = true; + } + } + for (int j = 0; j < SIZE; j++) + grid[i][j] = temp[j]; + } + return moved; +} + +// Rotate grid 90° clockwise +void rotateGrid() { + int temp[SIZE][SIZE]; + for (int i = 0; i < SIZE; i++) + for (int j = 0; j < SIZE; j++) + temp[j][SIZE - 1 - i] = grid[i][j]; + for (int i = 0; i < SIZE; i++) + for (int j = 0; j < SIZE; j++) + grid[i][j] = temp[i][j]; +} + +// Move tiles based on arrow key +bool move(int key) { + bool moved = false; + switch(key) { + case 72: // up arrow + rotateGrid(); rotateGrid(); rotateGrid(); + moved = slideLeft(); + rotateGrid(); + break; + case 80: // down arrow + rotateGrid(); + moved = slideLeft(); + rotateGrid(); rotateGrid(); rotateGrid(); + break; + case 75: // left arrow + moved = slideLeft(); + break; + case 77: // right arrow + rotateGrid(); rotateGrid(); + moved = slideLeft(); + rotateGrid(); rotateGrid(); + break; + } + return moved; +} + +// Check if game is over +bool gameOver() { + for (int i = 0; i < SIZE; i++) + for (int j = 0; j < SIZE; j++) + if (grid[i][j] == 0) return false; + + for (int i = 0; i < SIZE; i++) + for (int j = 0; j < SIZE-1; j++) + if (grid[i][j] == grid[i][j+1]) return false; + + for (int j = 0; j < SIZE; j++) + for (int i = 0; i < SIZE-1; i++) + if (grid[i][j] == grid[i+1][j]) return false; + + return true; +} + +int main() { + srand(time(0)); + initGrid(); + addRandomTile(); + addRandomTile(); + printGrid(); + + while (true) { + cout << "Use arrow keys to move tiles.\n"; + int ch = _getch(); // first input + if (ch == 224) { // arrow keys are detected in 2 steps + int arrow = _getch(); // second input gives actual key code + if (move(arrow)) { + addRandomTile(); + printGrid(); + } else { + cout << "Invalid move or no tiles moved.\n"; + } + } + + if (gameOver()) { + cout << "Game Over!\n"; + break; + } + } + + return 0; +}