Solved

how to capitalize first letter of a word

Posted on 2006-11-30
13
804 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
 
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MaxSpan challenge 9 67
triangle challenge 4 79
Path of Workbook 3 45
C# code editing and collaboration 3 49
Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
This is an explanation of a simple data model to help parse a JSON feed
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
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 …

746 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

12 Experts available now in Live!

Get 1:1 Help Now