What is a short code to make the first letter of a word capital automatically.
Announcement
Collapse
No announcement yet.
First Letter Caps
Collapse
X
-
Or:
Code:temp$ = UCASE$(LEFT$(temp$, 1)) + MID$(temp$, 2)
Egbert Zijlema, journalist and programmer (zijlema at basicguru dot eu)
http://zijlema.basicguru.eu
*** Opinions expressed here are not necessarily untrue ***
Comment
-
You must be thinking of some other compiler, there is no MCase() function in the PB/DOS compilers
However, here is a PB/DOS function equivalent:
Code:FUNCTION MCase( BYVAL sString AS STRING ) AS STRING DIM nPosition AS INTEGER DIM nWordBegin AS INTEGER DIM nWordEnd AS INTEGER DIM sMixedCase AS STRING DIM sWord AS STRING LET sMixedCase = sString LET nPosition = 1 DO UNTIL nPosition > LEN( sMixedCase ) LET nWordBegin = 0 LET nWordEnd = 0 'Find the start of a word DO WHILE INSTR( MID$( sMixedCase, nPosition, 1 ), ANY " ." ) > 0 AND nPosition <= LEN( sMixedCase ) INCR nPosition LOOP IF nPosition <= LEN( sMixedCase ) THEN LET nWordBegin = nPosition 'Find the end of the word DO INCR nPosition LOOP UNTIL INSTR( MID$( sMixedCase, nPosition, 1 ), ANY " ." ) > 0 OR nPosition > LEN( sMixedCase ) IF nPosition <= LEN( sMixedCase ) THEN LET nWordEnd = nPosition - 1 IF nWordBegin > 0 AND nWordEnd = 0 THEN LET nWordEnd = LEN( sMixedCase ) IF nWordBegin > 0 AND nWordEnd > 0 THEN LET sWord = MID$( sMixedCase, nWordBegin, nWordEnd - nWordBegin + 1 ) LET sWord = LCASE$( sWord ) LET sWord = UCASE$( LEFT$( sWord, 1 ) ) + MID$( sWord, 2 ) MID$( sMixedCase, nWordBegin, nWordEnd - nWordBegin + 1 ) = sWord END IF LOOP FUNCTION = sMixedCase END FUNCTION
If you try to make something idiot-proof, someone will invent a better idiot.
Comment
-
'Tested using PB/CC not DOS, please save work before running
Code:FUNCTION PBMAIN () AS LONG PRINT Cap("test") s$ = "hello" PRINT cap(s$) WAITKEY$ END FUNCTION FUNCTION Cap(sTemp AS STRING) AS STRING char& = PEEK(STRPTR(sTemp)) IF char& > 96 AND char& < 123 THEN POKE STRPTR(sTemp), char&-32 FUNCTION = sTemp END FUNCTION
Code:FUNCTION Cap2(sTemp AS STRING) AS STRING char& = ASC(sTemp) IF Char& > 96 AND char& < 123 THEN ASC(sTemp,1) = char& -32 FUNCTION = sTemp END FUNCTION
Last edited by Mike Doty; 30 Nov 2008, 01:36 PM.
Comment
-
First letter of string or each word?
Obviously, my last post didn't come through properly. My routine was written back in PB/DOS 2.1 and ran until my current version of 3.00c. It automatically capitalizes the first letter of EVERY word (or what passes for it). If you just wanted the first letter of the string, then MID$ will work. If you wanted the first letter of every word, then my routine will do the job. Here it is again:
Code:FUNCTION CapitalizeNextLetter$(EnteredKeystroke$, TempInputStringLength?) STATIC CapitalizeLetter?? LOCAL IsNextLetterCapitalized??, IsItUppercase??, IsItLowercase?? LOCAL IsItALetter??, Logic1?? SELECT CASE ASC(EnteredKeystroke$) CASE 32?, 33?, 44?, 45?, 46?, 63?, 64? ' Word break characters IsItALetter?? = &H0000 IsNextLetterCapitalized?? = &HFFFF CASE 65? TO 90?, 97? TO 122? ' "A" - "Z", "a" - "z" IsItALetter?? = &HFFFF IsNextLetterCapitalized?? = &H0000 SELECT CASE ASC(EnteredKeystroke$) CASE 65? TO 90? ' Uppercase letters IsItUppercase?? = &HFFFF IsItLowercase?? = &H0000 CASE 97? TO 122? ' Lowercase letters IsItUppercase?? = &H0000 IsItLowercase?? = &HFFFF END SELECT CASE ELSE IsItALetter?? = &H0000 IsNextLetterCapitalized?? = &H0000 END SELECT Logic1?? = (TempInputStringLength? = 1? OR IsNextLetterCapitalized??) IF Logic1?? AND NOT CapitalizeLetter?? THEN CapitalizeLetter?? = &HFFFF IF IsItALetter?? THEN IF CapitalizeLetter?? THEN IF IsItLowercase?? THEN EnteredKeystroke$ = UCASE$(EnteredKeystroke$) CapitalizeLetter?? = &H0000 ELSE IF IsItUppercase?? THEN EnteredKeystroke$ = LCASE$(EnteredKeystroke$) END IF END IF CapitalizeNextLetter$ = EnteredKeystroke$ END FUNCTION FUNCTION CapitalizeString$(SourceString$) LOCAL TempInputStringLength?, Result$, EnteredKeystroke$ IF LEN(SourceString$) > 0? THEN FOR TempInputStringLength? = 1? TO LEN(SourceString$) EnteredKeystroke$ = MID$(SourceString$, TempInputStringLength?, 1?) Result$ = Result$ + CapitalizeNextLetter$(EnteredKeystroke$, _ TempInputStringLength?) NEXT TempInputStringLength? ELSE TempInputStringLength? = 0? DO WHILE NOT INSTAT WEND EnteredKeystroke$ = INKEY$ SELECT CASE EnteredKeystroke$ CASE CHR$(&H08) ' backspace IF TempInputStringLength? > 0? THEN DECR TempInputStringLength? IF TempInputStringLength? > 0? THEN Result$ = LEFT$(Result$, TempInputStringLength?) ELSE Result$ = "" END IF END IF CASE > CHR$(&H1F) ' printable ASCII INCR TempInputStringLength? Result$ = Result$ + CapitalizeNextLetter$(EnteredKeystroke$, _ TempInputStringLength?) END SELECT LOOP UNTIL EnteredKeystroke$ = CHR$(&H0D) END IF CapitalizeString$ = Result$ END FUNCTION
Comment
-
'Not sure why every first word would be capitalized
'This would use less string manipulations.
Code:FUNCTION PBMAIN () AS LONG ? CapAll("now is the time for all good men.") WAITKEY$ END FUNCTION FUNCTION CapAll(sTemp AS STRING) AS STRING '97 to 122 only capitalize LOCAL SpacePosition AS LONG LOCAL PositionAfterSpace AS LONG LOCAL Char AS LONG char = ASC(sTemp) 'check first position of string IF char > 96 AND char < 123 THEN MID$(sTemp,1) = CHR$(char-32) DO SpacePosition = INSTR(SpacePosition+1,sTemp,CHR$(32)) 'search for a space IF SpacePosition THEN PositionAfterSpace = SpacePosition + 1 IF PositionAfterSpace <= LEN(sTemp) THEN 'examine character after space char = ASC(MID$(sTemp,PositionAfterSpace,1)) IF Char > 96 AND Char < 123 THEN MID$(sTemp,PositionAfterSpace,1) = CHR$(char-32) END IF END IF END IF LOOP WHILE SpacePosition FUNCTION = sTemp END FUNCTION
Last edited by Mike Doty; 30 Nov 2008, 03:33 PM.
Comment
-
Reason for first letter of every word capitalized
I have written applications that keep track of my music collection, such as the track title, artist, album title, and so on. I tend to prefer that the first letter of every word is capitalized with these types of data. Of course you could write:
Var1$ = LCASE$(Var1$)
MID$(Var1$, 1, 1) = UCASE$(MID$(Var1$, 1, 1))
But my routines make it easier to auto-capitalize the first letters of all "words" in the string. Just thought I'd help.
Comment
Comment