CODE BOARD TECHNOLOGIES – Fresher Interview Questions with answers

CODE BOARD TECHNOLOGIES – Fresher Interview Questions with answers

This blog explains about CODE BOARD TECHNOLOGIES – Fresher Interview Questions with answers  . Some of the questions are explained clearly well below :

_______________________________________________________________________________

1. Write java program to count words without inbuilt functions ? 

INPUT: code board is best code board is best

OUTPUT: code:2 board:2 is:2 best:2 

Length Of The String Without Using Java Built In Length Method :

Java Code With Example

 
The length of the string is easy to define , it is the number of characters in the string including white spaces .
Java make the things easy as it provides the built in length method for the String class (java.lang.String) which is a final class .
for example :
String demo = ” This is java built in method example “
int length= demo.length();
To use the java built in method to calculate the length of the string .
Then there is also a way to calculate the length of the string

You can count characters in two ways.

  1. Including space
  2. Excluding space

Below there are 2 programs to count characters in a string, I hope this might help you.

1.Program to count characters in a string including space

  1. import java.util.Scanner;
  2. public class Program
  3. {
  4. public static void main(String[] args)
  5. {
  6. System.out.println(“Enter the String”);
  7. Scanner scan=new Scanner(System.in);
  8. String input = scan.nextLine();
  9. char [] ch = input.toCharArray(); // converts string into character array
  10. System.out.println(ch.length); // prints the character count including space
  11. }
  12. }

2.Program to count characters in a string Excluding space

  1. import java.util.Scanner;
  2. public class Program

  3. {

  4. public static void main(String[] args)

  5. {

  6. System.out.println(“Enter the String”);

  7. Scanner scan=new Scanner(System.in);

  8. String input = scan.nextLine();

  9. char [] ch = input.toCharArray();

  10. int count=0;

  11. for (int i = 0; i < ch.length; i++)

  12. {

  13. if (ch[i]!=‘ ‘)

  14. {

  15. count++;

  16. }

  17. }

  18. System.out.println(count++);

  19. }

  20. }

_______________________________________________________________________________

REFERENCES :

https://www.quora.com/How-do-I-count-characters-in-a-string-in-Java-without-using-charcount