Solved

how to capitalize first letter of a word

Posted on 2006-11-30
13
823 Views
Last Modified: 2012-06-27
Hi guys I need a method to which if I ass a word it caitalize the first letter and send it back

for e.g. if I send alabama it sends me Alabama

Thanks
0
Comment
Question by:huzefaq
13 Comments
 
LVL 1

Expert Comment

by:lmedwilliams
ID: 18049638
Try this, you can add trim() to clean up empty spaces, but this should do what you like.

//---------------------------------------------------------------------------
#include <string>
#include <iostream>
#pragma hdrstop
using namespace std;
//---------------------------------------------------------------------------

#pragma argsused

string CapitalizeFirst(string inStr)
{
  inStr[0]=toupper(inStr[0]);
  return(inStr);
}

int main(int argc, char* argv[])
{
  string outStr="alabama";
  cout << "Original Word=" << outStr << endl;
  outStr = CapitalizeFirst(outStr);
  cout << "Finished Word=" << outStr << endl;
  system("PAUSE");
  return 0;

}
//---------------------------------------------------------------------------

0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 18050090
what programming language are you using?

AW
0
 
LVL 2

Expert Comment

by:joshpowers
ID: 18050132
what language would you like it in?

in c++ it would be something like

void TitleCase(char* word)
{
     char[0] &= 0xDF;
}

That should turn any lowercase letter into a captial letter and leave captial letters alone.  The same principle applies to other languages.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 9

Expert Comment

by:jhshukla
ID: 18050293
@joshpowers
we need to check for valid range too. don't want to convert {|} to [\], etc. do we?
0
 
LVL 41

Expert Comment

by:HonorGod
ID: 18050710
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title> Leading Cap </title>
<script type='text/javascript'>  
  function cap() {
    return arguments[ 0 ].toUpperCase();
  }

  function doit( obj ) {
    obj.value = obj.value.replace( /\b([a-z])/, cap );
  }
</script>  
</head>
<body>
  <input type='text' onchange='doit(this);return false;' />
</body>
</html>
0
 
LVL 18

Accepted Solution

by:
Ravi Singh earned 150 total points
ID: 18050901
Based on your PAQ's -- a Java method that returns the new string:

    public String capitalizeFirstLetter(String input)
    {
        if (input.trim().equals("")) return "";
        else return input.substring(0, 1).toUpperCase() + input.substring(1, input.length());
    }
0
 
LVL 2

Expert Comment

by:joshpowers
ID: 18051412
@jhshukla

Clearly some checking of your input is often nessisary when programming.  However the question was how do I turn a lower case letter into an uppercase letter.  My solution is a one liner that will work in nearly any language without many modifications.
Anyways the main point is that if you dont know what your variables ranges are then check them, if you have already checked them at other points then dont.  It is all about managing the data.
0
 

Expert Comment

by:leroopsin
ID: 18053925
Dear All,

If you want to do in VBScript, you give you a function in VBScript.

Function TitleCase(DaString)
    if not DaString = "" or IsNull(DaString) then
        dim DaTempString, DaArray, ConVStr
        DaString = lcase(DaString)
        DaArray = Split(DaString," ")
        For i = 0 to ubound(DaArray)
        ConVStr = UCase(Left(DaArray(i),1)) + mid(DaArray(i),2)
        DaTempString = DaTempString & " " & ConVStr
        next
    else
        DaTempString = "Nothing to Convert"
    end if
    TitleCase = DaTempString
End Function

Best regards,

Sin Le
0
 

Assisted Solution

by:alisadaqain
alisadaqain earned 100 total points
ID: 18058559
I guess Java is missing so here it is.

package whatever;

public class FirstCharChangeCase {

      public static void main(String[] args) {
            
            String Apple = "alabama";
            String First = null;
            String Rest = null;
            String appleAfter = null;
            
            //Break up string
            First = Apple.substring(0,1);
            Rest = Apple.substring(1);
            
            //Change First to UPPER
            First = First.toUpperCase();
            
            //Final Product
            appleAfter = First+Rest;
            
            //Test Print
            System.out.println(Apple);
            System.out.println(First);
            System.out.println(Rest);
            System.out.println(appleAfter);
      }

}

FYI...Used Java 1.6
0
 
LVL 7

Expert Comment

by:mquiroz
ID: 18074075
and c#

        string capitalize(string word)
        {
            return (word.Trim().Substring(0, 1) + word.Trim().Substring(1));
        }
0
 
LVL 7

Expert Comment

by:mquiroz
ID: 18074131
sorry, the best part was missing ;-P

        static string capitalize(string word)
        {
            return (word.Trim().Substring(0, 1).ToUpper() + word.Trim().Substring(1));
        }
0
 
LVL 1

Expert Comment

by:m00958
ID: 18080117
In VB you could use:

Public Function Capitalise(Value as String) as String
    Capitalise = StrConv(Value, vbProperCase)
End Function
0
 
LVL 7

Expert Comment

by:mquiroz
ID: 18097891
it's a nice collection of different languages aproach to the same problem, some kind of programmers' rosetta stone ;-)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
This article will show, step by step, how to integrate R code into a R Sweave document
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now