
Kim L.
asked 01/23/21I have a coding question
How do you capitalize the first character of each word in a string in an array?
Explain with a simple example please?
Thanks
1 Expert Answer
//******* JAVA ********//
class CamelCase
{
public String Go( String str)
{
String tokens[] = str.split(" ");
String returnStr=" ";
String newStr=null;
for (int iLoop=0; iLoop<tokens.length; iLoop++)
{
String curStr = tokens[iLoop];
byte byteBuff[] = curStr.getBytes();
char firstChar = (char) byteBuff[0];
char chCharCapitalized = Character.toUpperCase(firstChar);
byteBuff[0]= (byte)chCharCapitalized;
newStr = new String(byteBuff);
returnStr += newStr;
returnStr += " ";
}
return(returnStr.trim());
}
public static void main(String args[])
{
CamelCase camelCase = new CamelCase();
String str = new String("the quick brown fox jumped over the lazy dogs ");
String strCamelCase = camelCase.Go(str);
System.out.println(strCamelCase);
}
}
//***** C/C++ *****************************************//
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
CamelCase(char * inbuff, char * outbuff)
{
char * token = strtok(inbuff," ");
while (token != NULL)
{
char firstChar = token[0];
token[0]=toupper(firstChar);
printf("parsed token:>%s< \n",token);
strcat(outbuff,token);
strcat(outbuff," ");
token = strtok(NULL," ");
}
}
int main(int argc, char * argv[])
{
char inbuff[] = {"the quick brown fox jumped over the lazy dogs"};
char outbuff[255];
memset(outbuff,0,255);
CamelCase(inbuff,outbuff);
printf("Final Result: >%s< \n",outbuff);
}
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
01/23/21