0% found this document useful (0 votes)
5 views6 pages

String Problems

The document outlines various string manipulation problems and their solutions in Java, including counting word occurrences, dividing strings, finding the length of the last word, and swapping strings without a temporary variable. It also covers identifying strings with matching first and last characters, finding the first non-repeating character, removing duplicates, and counting character occurrences. Each problem is accompanied by a code example demonstrating the solution.

Uploaded by

amansharma807737
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views6 pages

String Problems

The document outlines various string manipulation problems and their solutions in Java, including counting word occurrences, dividing strings, finding the length of the last word, and swapping strings without a temporary variable. It also covers identifying strings with matching first and last characters, finding the first non-repeating character, removing duplicates, and counting character occurrences. Each problem is accompanied by a code example demonstrating the solution.

Uploaded by

amansharma807737
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

String Problems

1. Count the occurrence of each word in a String


2. Divide a String into n equal parts
3. Find the length of the last word in a String
4. Swap two Strings without using a temporary variable
5. Write a program where the first and last characters must be the same in a String array
6. Find the first non-repeating character in a String//?
7. Find the longest substring without repeating characters//?
8. Reverse a String in place and reverse the whole String
9. Reverse a String in place only
10. Reverse a substring in a String
11. Write a program to count the length of a String
12. Remove duplicates from a String
13. Count the occurrence of each character in a String
14. Remove vowels

Count the occurrence of each word in a String

import [Link];

class WordOccurrence {
public static void main(String[] args) {
String input = "This is a test string. This string is for testing.";
findWordOccurrences(input);
}
public static void findWordOccurrences(String input) {

input = [Link]().replaceAll("[^a-zA-Z0-9\\s]", "");


String[] words = [Link](" ");
[Link](words);
int count = 1;
for (int i = 1; i < [Link]; i++) {
if (words[i].equals(words[i - 1])) {

count++;
} else {

[Link](words[i - 1] + ": " + count);

count = 1;
}
}
[Link](words[[Link] - 1] + ": " + count }
}
Divide a String into n equal parts

class DivideString {
public static void main(String[] args) {
String input = "abcdefghijklmnopqrstuvwx";
int n = 4;
divideString(input, n);
}

public static void divideString(String input, int n) {


int len = [Link]();
int partSize = len / n;

if (len % n != 0) {
[Link]("The string cannot be divided into " + n + " equal
parts.");
return;
}

for (int i = 0; i < len; i += partSize) {


String part = [Link](i, i + partSize);
[Link](part);
}
}
}

Find the length of the last word in a String

class A{
public static void main(String[] args) {
String s1 ="Hello World";
int count =0;
for(int i=[Link]()-1;i>=0;i--){
if ([Link](i)==' ') {
break;
}
else{
count++;
}
}
[Link](count);

}}
// Swap without temp

public class SwapStrings {


public static void main(String[] args) {
String str1 = "Hello";
String str2 = "World";

str1 = str1 + str2;


str2 = [Link](0, [Link]() - [Link]());
str1 = [Link]([Link]());
[Link]("After swapping:");
[Link]("str1: " + str1);
[Link]("str2: " + str2);
}
}

// First and last character must be same


class A{
public static void main(String[] args) {
String s1= "madam radar and level";
s1=[Link]();
String s2[] = [Link](" ");
for(int i=0;i<[Link];i++){
if (s2[i].charAt(0)==s2[i].charAt(s2[i].length()-1)) {
[Link](s2[i]);
}
}

}
}
//find first non-repeating character

class A {
public static void main(String[] args) {
String s1 = "swiss";
boolean flag;
char ch = '\0';

for (int i = 0; i < [Link](); i++) {


flag = false;

for (int j = 0; j < [Link](); j++) {

if (i != j && [Link](i) == [Link](j)) {


flag = true;
break;
}
}

if (!flag) {
ch = [Link](i);
break;
}
}

if (ch != '\0') {
[Link]("The first non-repeating character is: " + ch);
} else {
[Link]("No non-repeating character found.");
}
}
}
//remove duplicate
class RemoveDuplicates {
public static void main(String[] args) {
String input = "swiss";
String result = removeDuplicates(input);
[Link]("String after removing duplicates: " + result);
}

public static String removeDuplicates(String str) {


StringBuilder output = new StringBuilder();

for (int i = 0; i < [Link](); i++) {


char currentChar = [Link](i);

if ([Link]([Link](currentChar)) == -1) {
[Link](currentChar);
}
}

return [Link]();
}
}
Count the occurrence of each character in a String

import [Link];

class hello12_Harsh{

public static void main(String[] args) {


String s1 ="preeti";

logic(s1);
}

public static void logic(String s1){


s1= [Link]().replaceAll("[^a-zA-Z0-9]", "");
String s2[] = [Link]("");
[Link](s2);
int count=1;
for(int i=1;i<[Link];i++){
if (s2[i].equals(s2[i-1])) {
count++;
} else {
[Link](s2[i-1]+" : "+count);
count=1;
}
}
[Link](s2[[Link]-1]+" : "+ count);

You might also like