Java Code for Proper Case of a String without Uppercase / Lowercase Function.

Proper function in another language / package makes the string proper case ie. First Character of each word will be capital letter. Remaining letters will be in small. The following code accepts strings from keyboard and converts the string to proper case.


package com.javaonline;

import java.util.Scanner;

import java.io.*;

class Proper

{

public static void main(String [] args)

{

try

{

System.out.println("Enter any Line");

Scanner sc= new Scanner(System.in);

String line=sc.useDelimiter("\n").next();

String out=Proper(line);

System.out.println("Proper Case of the given line is : " + out);

}catch (Exception e){}

}

public static String Proper(String line) throws IOException{

StringReader in = new StringReader(line);

boolean isNextCharSpace = true;

StringBuffer proper = new StringBuffer();

int i=0;

while((i=in.read())!=-1)

{

char c = (char)i;

if (c == ' ')

{

proper.append(c);

isNextCharSpace = true;

}

else

{

if (isNextCharSpace)

{

if(i>=97 && i<=122)

{

i=i-32;

proper.append((char)i);

}

else

{

proper.append(c);

}

}

else

{

if(i>=65 && i<=90)

{

i=i+32;

proper.append((char)i);

}

else

{

proper.append(c);

}

}

isNextCharSpace = false;

}

}

return proper.toString();

}

} 

Running the program will give you the Proper case of the given input.

proper output

Leave a Reply