Professional Documents
Culture Documents
// Main class
class GFG {
Output
First occurrence of char 's' is found at : 4
Last occurrence of char 's' is found at : 28
First occurrence of char 's' after index 10 : 12
Last occurrence of char 's' after index 20 is : 15
Character at location 20: 111
// Main class
class GFG {
Output
First occurrence of char Geeks is found at : 0
Last occurrence of char Geeks is found at : 8
First occurrence of char Geeks after index 10 : -1
Last occurrence of char Geeks after index 20 is : 8
3. contains(CharSequence seq): It returns true if the string contains the specified
sequence of char values otherwise returns false. Its parameters specify the
sequence of characters to be searched and throw NullPointerException if seq is
null.
// Class
class GFG {
Output
Found soft?: true
Found war? true
Found wr?: false
boolean startsWith(String str): Returns true if the string str exists at the
starting of the given string, else false.
boolean startsWith(String str, int indexNum): Returns true if the string str exists
at the starting of the index indexNum in the given string, else false.
boolean endsWith(String str): Returns true if the string str exists at the ending
of the given string, else false.
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Input string in which substring
// is to be searched
String str
= "GeeksforGeeks is a computer science portal";
SUBSTRINGS
Output
9
class GFG
{
// Driver code
public static void main(String args[])
{
String str = "geeksforgeeks";
int n = 5;
System.out.print(countSubStr(str, n));
}
}
Output
9
2. Given a string str and a character X. The task is to find the total number of
sub-strings that contain the character X at least once.
return res;
}
// Driver code
public static void main(String[] args)
{
String str = "abcabc";
int n = str.length();
char x = 'c';
System.out.println(countSubStr(str, n, x));
}
}
class GFG
{
//sort(substrings.begin(), substrings.end());
}
}
// Driver code
public static void main(String args[])
{
String s = "geek";
int queries[] = { 1, 5, 10 };
int q = queries.length;
// Perform queries
for (int i = 0; i < q; i++)
System.out.println(substrings[queries[i]-1]);
}
}
Examples:
class GFG
{
// Driver Code
public static void main(String []args)
{
String s = "aaacaabbaa";
int k = 2;
System.out.println(maxSubStrings(s, k));
}
}
Output
3
class GFG
{
// Driver code
public static void main(String[] args)
{
String s1 = "aab", s2 = "aaaab";
System.out.println(countSubstrs(s1, s2));
}
}
Output
6
// Java implementation
// of above approach
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{
// Function to print the
// substrings of a number
static void printSubstrings(int n)
{
// Calculate the total
// number of digits
int s = (int)Math.log10(n);
while (n > 0)
{
// Driver code
public static void main(String args[])
{
int n = 123;
printSubstrings(n);
}
}
Output:
1
12
123
2
23
3
// Driver Code
public static void main(String []args)
{
char[] s = "geeks".toCharArray();
int n = s.length;
printPattern(s, n);
}
}
Input: S = "471", X = 47
Output: 2
Only the sub-strings "471" and "71"
satisfy the given conditions.
Input: S = "2222", X = 97
Output: 3
Valid strings are "222", "222" and "2222".
class GFG
{
// Function that counts
// valid sub-strings
static int count(String S, int X)
{
int count = 0;
int N = S.length();
for (int i = 0; i < N; ++i)
{
// Driver code
public static void main(String []args)
{
String S = "2222";
int X = 97;
System.out.println(count(S, X));
}
}
Output
3
Examples:
class GFG
{
// No occurrence found
return -1;
}
// Driver code
static public void main ( String []arg)
{
String str = "geeksforgeeks";
int n = str.length();
char ch = 'k';
System.out.println(countSubStr(str, n, ch));
}
}
Output:
56