Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,148,893 members, 7,802,876 topics. Date: Saturday, 20 April 2024 at 12:39 AM

Capitalise First Letter Of Words In A String - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Capitalise First Letter Of Words In A String (2385 Views)

[MOD] PHP WORKOUTS - String Manipulation 1 / How Copy Strings From Stringerbuilder To...a String Array / Troubleshooting A String In Javame (2) (3) (4)

(1) (Reply) (Go Down)

Capitalise First Letter Of Words In A String by megaplaza(m): 9:57pm On Jul 21, 2011
please write a program that will take a string, convert all the letters to lower case then capitalise the first letter of each word in the string. you program should be similar to the function of ucword() in php. i expect the program t be written in java or other programming language. please don't use any inbuilt function that handles such task. here is a sample of my solution

for instance if you have a string "i aM iNNocent, Please leave ME ALONE", your program should output "I Am Innocent, Please Leave Me Alone"

package javaapplication17;

/**
*
* @author Benedine
*/
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String str = "yOu Are TYY ode, iQWW iThaNks";

            String words = str.toLowerCase();

        StringBuilder upperCasedWords = new StringBuilder(words.length());

        String[] word = words.split("\\s"wink;
        for(int i=0,l=word.length;i<l;++i) {

            if(i>0) upperCasedWords.append(" "wink;
                upperCasedWords.append(Character.toUpperCase(word[i].charAt(0))).append(word[i].substring(1));

        }


        System.out.println(upperCasedWords.toString());
    }

}
Re: Capitalise First Letter Of Words In A String by Nobody: 11:11pm On Jul 21, 2011
Re: Capitalise First Letter Of Words In A String by megaplaza(m): 12:34am On Jul 22, 2011
thanks for the comment, necessity is the mother of invention, i have a need for this in an application that am developing, i wanted to make sure that data stored on the db are capitalised and , i could find a function in java that does this right away for you. so , but i did a lot of research online though. so credit to those authors.
Re: Capitalise First Letter Of Words In A String by IBEXY(m): 12:47pm On Jul 22, 2011
megaplaza:
please don't use any inbuilt function that handles such task. here is a sample of my solution

and what have you done in the lines below. I dont know Java but those look like case conversion functons to me. I was curious to see how you could achieve this feat woithout using them.

String words = str.toLowerCase();
upperCasedWords.append(Character.toUpperCase(word[i].charAt(0))).append(word
Re: Capitalise First Letter Of Words In A String by megaplaza(m): 6:30pm On Jul 22, 2011
what i meant by using in-built function is using a funtion that wwhen you use it on string, it will perform wwhat this code does e.g 'i aM DOne'.strToUpperCaseFirstWord(); then this will output 'I Am Done'. I don't know of any such function and am saying you should use it if it exist that will lead you to write the block of code above. For instance in php strtolower(str) or so changes the whole str letters to lower case, strtoupper(str) changes the whole string to upper case. Then php has a functions ucwords(str) that will capitalise all first letter of the words in string. The is the desire function i want your codee to perform and am sayin if there is a java equivalent of ucwords() pls dont use it rather use tolower() and toUpper() tom implement your own ucwords(). Hope this explains it, remember am not trying to show my prowess, i happened to need that to capitalise every word in a string in my program and i noticed that java doesn't have such function, so after coming up with this i decided to share it here. case, strtoupper(str) changes the whole string to upper case. Then php has a functions ucwords(str) that will capitalise all first letter of the words in string. The is the desire function i want your codee to perform and am sayin if there is a java equivalent of ucwords() pls dont use it rather use tolower() and toUpper() tom implement your own ucwords(). Hope this explains it, remember am not trying to show my prowess, i happened to need that to capitalise every word in a string in my program and i noticed that java doesn't have such function, so after coming up with this i decided to share it here.

(1) (Reply)

I Need Help With Pointers In C / Android App Programming! / Node.js Vs Php

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 15
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.