Program to Find Quotient and Remainder in C++

The division operator / computes the quotient (either between float or integer variables). The modulus operator % computes the remainder when one integer is divided by another (modulus operator cannot be used for floating-type variables).

Find the quotient and remainder of a given dividend and divisor. In this program, the user is asked to enter two integers (divisor and dividend) and the quotient and the remainder of their division is computed. To compute quotient and remainder, both divisor and dividend should be integers.

[mai mult...]

Program to Add Two Numbers in C++

User is asked to enter two integers. Then, the sum of those two integers is stored in a variable and displayed on the screen. Primary tabs

In this program, the user is asked to enter two integers.These two integers are stored in variables first_number and second_number respectively.Then, the variables are added using the + operator and stored in the sum variable. Finally, sum is displayed on the screen.

[mai mult...]

Replace the spaces of a string with a specific character using Java

  1. public class ReplaceSpace
  2. {
  3.     public static void main(String[] args) {
  4.         String string = “Once in a blue moon”;
  5.         char ch = ‘-‘;
  6.         //Replace space with specific character ch  
  7.         string = string.replace(‘ ‘, ch);
  8.         System.out.println(“String after replacing spaces with given character: “);
  9.         System.out.println(string);
  10.     }
  11. }

Output:

String after replacing spaces with given character: 
Once-in-a-blue-moon
[mai mult...]

Find the most repeated word in a text file with Java

  1. import java.io.BufferedReader;
  2. import java.io.FileReader;
  3. import java.util.ArrayList;
  4. public class MostRepeatedWord {
  5.     public static void main(String[] args) throws Exception {
  6.         String line, word = “”;
  7.         int count = 0, maxCount = 0;
  8.         ArrayList<String> words = new ArrayList<String>();
  9.         //Opens file in read mode  
  10.         FileReader file = new FileReader(“data1.txt “);
  11.         BufferedReader br = new BufferedReader(file);
  12.         //Reads each line  
  13.         while((line = br.readLine()) != null) {
  14.             String string[] = line.toLowerCase().split(“([,.\\s]+) “);
  15.             //Adding all words generated in previous step into words  
  16.             for(String s : string){
  17.                 words.add(s);
  18.             }
  19.         }
  20.         //Determine the most repeated word in a file  
  21.         for(int i = 0; i < words.size(); i++){
  22.             count = 1;
  23.             //Count each word in the file and store it in variable count  
  24.             for(int j = i+1; j < words.size(); j++){
  25.                 if(words.get(i).equals(words.get(j))){
  26.                     count++;
  27.                 }
  28.             }
  29.             //If maxCount is less than count then store value of count in maxCount   
  30.             //and corresponding word to variable word  
  31.             if(count > maxCount){
  32.                 maxCount = count;
  33.                 word = words.get(i);
  34.             }
  35.         }
  36.         System.out.println(“Most repeated word: “ + word);
  37.         br.close();
  38.     }
  39. }

Output:

Most repeated word: word
[mai mult...]

Find the frequency of characters with Java

To accomplish this task, we will maintain an array called freq with same size of the length of the string. Freq will be used to maintain the count of each character present in the string. Now, iterate through the string to compare each character with rest of the string. Increment the count of corresponding element in freq. Finally, iterate through freq to display the frequencies of characters.

For example: Frequency of p in above string is 2.

[mai mult...]

How to Reverse a String in Java

Example 1: Reverse a string word by word using recursion

  1. import java.util.Scanner;
  2. public class ReverseStringExample1
  3. {
  4. public static void main(String[] args)
  5. {
  6. String str;
  7. System.out.println(“Enter a string: “);
  8. Scanner scanner = new Scanner(System.in);
  9. str = scanner.nextLine();
  10. scanner.close();                                //closes the input stream
  11. String reversed = reverseString(str);
  12. System.out.println(“The reversed string is: “ + reversed);
  13. }
  14. public static String reverseString(String s)
  15. {
  16. if (s.isEmpty())                            //checks the string if empty
  17. return s;
  18. return reverseString(s.substring(1)) + s.charAt(0);                     //recursively called function
  19. }
  20. }

Output:

How to Reverse a String in Java Word by Word

[mai mult...]