Need logic to make first letter caps?

Time Sharing Option, Interactive System Productivity Facility and REstructured eXtended eXecutor

Moderator: mickeydusaor

Post Reply
User avatar
Akshya Chopra
Registered Member
Posts: 77
Joined: Mon May 20, 2013 11:32 pm
Algeria

Need logic to make first letter caps?

Post 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.
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: Need logic to make first letter caps?

Post 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
zprogrammer
User avatar
Akshya Chopra
Registered Member
Posts: 77
Joined: Mon May 20, 2013 11:32 pm
Algeria

Re: Need logic to make first letter caps?

Post 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.
User avatar
Akshya Chopra
Registered Member
Posts: 77
Joined: Mon May 20, 2013 11:32 pm
Algeria

Re: Need logic to make first letter caps?

Post 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.
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: Need logic to make first letter caps?

Post 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
zprogrammer
User avatar
Akshya Chopra
Registered Member
Posts: 77
Joined: Mon May 20, 2013 11:32 pm
Algeria

Re: Need logic to make first letter caps?

Post 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?
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: Need logic to make first letter caps?

Post 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?
zprogrammer
User avatar
Akshya Chopra
Registered Member
Posts: 77
Joined: Mon May 20, 2013 11:32 pm
Algeria

Re: Need logic to make first letter caps?

Post 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.
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: Need logic to make first letter caps?

Post 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
zprogrammer
User avatar
Akshya Chopra
Registered Member
Posts: 77
Joined: Mon May 20, 2013 11:32 pm
Algeria

Re: Need logic to make first letter caps?

Post by Akshya Chopra »

Thanks Pandora Box.

Aprreciate your patience, this works for me.
zprogrammer
Global Moderator
Global Moderator
Posts: 588
Joined: Wed Nov 20, 2013 11:53 am
Location: Mars

Re: Need logic to make first letter caps?

Post by zprogrammer »

Glad it worked :)

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

Create an account or sign in to join the discussion

You need to be a member in order to post a reply

Create an account

Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute

Register

Sign in

Return to “TSO, ISPF & REXX (Do you still do CLIST?!).”