Assignment No 1
Assignment No 1
Assignment No: 01
str2=str1.substring(n,(m+n));
System.out.println(" Extracted String is : "+str2);
}
}
Qno 2: Write a java program to count the number of
occurrences of a particular character in entered string.
class GFG
{
// Method that return count of the given
// character in the string
public static int count(String s, char
c)
{
int res = 0;
for (int i=0; i<s.length(); i++)
{
// checking character in string
if (s.charAt(i) == c)
res++;
}
return res;
}
// Driver method
public static void main(String args[])
{
String str= "geeksforgeeks";
char c = 'e';
System.out.println(count(str, c));
}
}
class MergeTwoSorted
{
// Merge arr1[0..n1-1] and arr2[0..n2-1]
// into arr3[0..n1+n2-1]
public static void mergeArrays(int[] arr1, int[] arr2, int n1,
int n2, int[] arr3)
{
int i = 0, j = 0, k = 0;
// Traverse both array
while (i<n1 && j <n2)
{
// Check if current element of first
// array is smaller than current element
// of second array. If yes, store first
// array element and increment first array
// index. Otherwise do same with second array
if (arr1[i] < arr2[j])
arr3[k++] = arr1[i++];
else
arr3[k++] = arr2[j++];
}
Hint : use the modulus operator to extract the last digit and the
integer division by 10 to get the n-1 digit number from the n digit
number.
NO IS - 12345
ANS IS 54321
1. public class ReverseNumberExample1
2. {
3. public static void main(String[] args)
4. {
5. int number = 987654, reverse = 0;
6. while(number != 0)
7. {
8. int remainder = number % 10;
9. reverse = reverse * 10 + remainder;
10. number = number/10;
11. }
12. System.out.println("The reverse of the given number is:
" + reverse);
13. }
14. }