Page 1 of 1

Need logic to make first letter caps?

Posted: Tue Feb 04, 2014 11:19 am
by Akshya Chopra
Hi,

Using REXX I need to make all the first letter CAPS, of a word in a sentence to be caps. For example, if input is:

Code: Select all

this is example.
Output:

Code: Select all

This is example.
I thought of using PARSE UPPER but it will convert the whole string to uppercase. Can someone please provide a pointer.

Re: Need logic to make first letter caps?

Posted: Tue Feb 04, 2014 12:02 pm
by zprogrammer
Try this,

Code: Select all

/*REXX*/
INPUT='this is a string'
LengthofString = LENGTH(INPUT)
OUTPUT=TRANSLATE(SUBSTR(INPUT,1,1))||SUBSTR(INPUT,2,LENGTHOFSTRING-1)
SAY ' output = ' output

Re: Need logic to make first letter caps?

Posted: Wed Feb 05, 2014 3:13 pm
by Akshya Chopra
Thanks Pandora - unfortunately I,m facing some problem with my system so can't check it. I'll get back to you soon.

Re: Need logic to make first letter caps?

Posted: Sun Feb 09, 2014 12:42 am
by Akshya Chopra
Thanks PB. I realized I've not put a correct example here and what I was looking for was like this:

Input:

Code: Select all

this is example.
Output:

Code: Select all

This Is Example.
Sorry for the wrong details before.

Re: Need logic to make first letter caps?

Posted: Mon Feb 10, 2014 2:25 pm
by zprogrammer
Try this

I have assumed the delimiter is space

Code: Select all

/*REXX*/
Input='this is a string'
X    = WORDS(Input)
   Output = ''
Do I = 1 to X
   Out.I = Subword(Input,I,1)
   Lengthofstring  = Length(Out.I)
   Out.I=Translate(Substr(Out.I,1,1))||Substr(Out.I,2,Lengthofstring-1)
   If I = 1 Then Do
      Output = Out.I
   End
   Else Do
      Output = Output || ' ' ||Out.I
   End
end
SAY '  Input = ' Input
SAY ' Output = ' Output
Output

Code: Select all

  Input =  this is a string
 Output =  This Is A String

Re: Need logic to make first letter caps?

Posted: Sun Feb 16, 2014 2:03 am
by Akshya Chopra
Thanks Pandora. This works however for a string like

Code: Select all

This is eXAMple
it gives the o/p as

Code: Select all

This is EXAMple
instead of

Code: Select all

This is Example
- any general routine we can write?

Re: Need logic to make first letter caps?

Posted: Sun Feb 16, 2014 2:40 am
by zprogrammer
Could you please eloborate the requirement?

Are you trying to convert the first character to UPPER and rest all to lower irrespective of the input?

Re: Need logic to make first letter caps?

Posted: Sun Feb 16, 2014 8:14 pm
by Akshya Chopra
Pandora-Box wrote:Are you trying to convert the first character to UPPER and rest all to lower irrespective of the input?
Yes, sorry for the changing description. I did not realize it until I kept on working with data.

Re: Need logic to make first letter caps?

Posted: Mon Feb 17, 2014 1:40 pm
by zprogrammer
Try this

Code: Select all

/*REXX*/
Input= 'This is eXAMple'
A2ZU = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
A2ZL = 'abcdefghijklmnopqrstuvwxyz'
X    = WORDS(Input)
   Output = ''
Do I = 1 to X
   Out.I = Subword(Input,I,1)
   Lengthofstring  = Length(Out.I)
   Out.I=Translate(Substr(Out.I,1,1))||,
         Translate(Substr(Out.I,2,Lengthofstring-1),A2ZL,A2ZU)
   If I = 1 Then Do
      Output = Out.I
   End
   Else Do
      Output = Output || ' ' ||Out.I
   End
End
Output

Code: Select all

 Input =  This is eXAMple
Output =  This Is Example

Re: Need logic to make first letter caps?

Posted: Thu Feb 20, 2014 11:41 am
by Akshya Chopra
Thanks Pandora Box.

Aprreciate your patience, this works for me.

Re: Need logic to make first letter caps?

Posted: Thu Feb 20, 2014 1:04 pm
by zprogrammer
Glad it worked :)

Note : I am working hard on my patience these days :lol: