470,648 Members | 1,618 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,648 developers. It's quick & easy.

Recursive function to change Char's in a String to Upper/lower

Hello,

I'm looking for a function for converting the input to a regular text.
Example: when the input is "aBCdefgHI jkLM ooPP" the return value of the
function should be "Abcdefghi Jklm Oopp"

Thanks in advance,

Anita

Nov 21 '05 #1
7 3145
GysAnn wrote:
I'm looking for a function for converting the input to a regular text.
Example: when the input is "aBCdefgHI jkLM ooPP" the return value
of the function should be "Abcdefghi Jklm Oopp"

Dim oldText As String
Dim newText As String

'Get the original text
oldText = "aBCdefgHI jkLM ooPP"

'Convert to Proper Case
newText = StrConv(oldText, VbStrConv.ProperCase)

'Remove any double-spaces
Do While InStr(newText, " ") > 0
newtext = replace(newtext," "," ")
Loop

'All done
MsgBox(newText)

--

(O)enone

Nov 21 '05 #2

"Oenone" <no***@nowhere.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
GysAnn wrote:
I'm looking for a function for converting the input to a regular text.
Example: when the input is "aBCdefgHI jkLM ooPP" the return value
of the function should be "Abcdefghi Jklm Oopp"

Dim oldText As String
Dim newText As String

'Get the original text
oldText = "aBCdefgHI jkLM ooPP"

'Convert to Proper Case
newText = StrConv(oldText, VbStrConv.ProperCase)

'Remove any double-spaces
Do While InStr(newText, " ") > 0
newtext = replace(newtext," "," ")
Loop

'All done
MsgBox(newText)


StrConv with ProperCase enum will only uppercase the first letter in each
word in the phrase..

so...

Sub Main()
Dim text As String = "aBCdefgHI jkLM ooPP"

Console.WriteLine("Before ReCasing : " & text)
text = ReCaseString(text)
Console.WriteLine("After ReCasing : " & text)
text = RemoveDblSpaces(text)
Console.WriteLine("After Removing DblSpaces: " & text)
End Sub

Private Function ReCaseString(ByVal Text As String) As String
Dim chars As Char() = Text.ToCharArray()
Text = String.Empty

For Each ch As Char In chars
If Char.IsLower(ch)
ch = Char.ToUpper(ch)
Else
ch = Char.ToLower(ch)
End If

Text &= ch
Next

Return Text
End Function

Private Function RemoveDblSpaces(ByVal Text As String) As String
While Text.IndexOf(Space(2)) > 0
Text = Text.Replace(Space(2), Space(1))
End While

Return Text
End Function
Nov 21 '05 #3
Thanks, but i must be on an recursive way like :

myString = "aBCdefgHI jkLM ooPP" '(from InputBox by example)
myCorrectedStr = fncCorrectInput(myString)

Private Function fncCorrectInput(ByVal myString as String) As String
If ....
Return....
Else
Return .....(?) ...... & fncCorrectInput(myParameter)
End If
End Function
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #4
The function has to loop thrue the string in a recursive way (= no loops
Do... / For....) like :
myString = "aBCdefgHI jkLM ooPP" '(from InputBox by example)
myCorrectedStr = fncCorrectInput(myString)
' my corrected string is now : "Abcdefghi Jklm Oopp"

Private Function fncCorrectInput(ByVal myString as String) As String
If ....
Return....
Else
Return .....(?) ...... & fncCorrectInput(myParameter)
End If
End Function

"Oenone" <no***@nowhere.com> schreef in bericht
news:%2***************@TK2MSFTNGP10.phx.gbl...
GysAnn wrote:
I'm looking for a function for converting the input to a regular text.
Example: when the input is "aBCdefgHI jkLM ooPP" the return value
of the function should be "Abcdefghi Jklm Oopp"

Dim oldText As String
Dim newText As String

'Get the original text
oldText = "aBCdefgHI jkLM ooPP"

'Convert to Proper Case
newText = StrConv(oldText, VbStrConv.ProperCase)

'Remove any double-spaces
Do While InStr(newText, " ") > 0
newtext = replace(newtext," "," ")
Loop

'All done
MsgBox(newText)

--

(O)enone

Nov 21 '05 #5
GysAnn,
The function has to loop thrue the string in a recursive way (= no loops Sounds like a homework assignment.

(O)enone's method seems to be the easiest way to solve this problem.

I wish you well in the class!

Hope this helps
Jay

"GysAnn" <be****@NOSPAMtelenet.be> wrote in message
news:CH*********************@phobos.telenet-ops.be... The function has to loop thrue the string in a recursive way (= no loops
Do... / For....) like :
myString = "aBCdefgHI jkLM ooPP" '(from InputBox by example)
myCorrectedStr = fncCorrectInput(myString)
' my corrected string is now : "Abcdefghi Jklm Oopp"

Private Function fncCorrectInput(ByVal myString as String) As String
If ....
Return....
Else
Return .....(?) ...... & fncCorrectInput(myParameter)
End If
End Function

"Oenone" <no***@nowhere.com> schreef in bericht
news:%2***************@TK2MSFTNGP10.phx.gbl...
GysAnn wrote:
I'm looking for a function for converting the input to a regular text.
Example: when the input is "aBCdefgHI jkLM ooPP" the return value
of the function should be "Abcdefghi Jklm Oopp"

Dim oldText As String
Dim newText As String

'Get the original text
oldText = "aBCdefgHI jkLM ooPP"

'Convert to Proper Case
newText = StrConv(oldText, VbStrConv.ProperCase)

'Remove any double-spaces
Do While InStr(newText, " ") > 0
newtext = replace(newtext," "," ")
Loop

'All done
MsgBox(newText)

--

(O)enone


Nov 21 '05 #6
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb:
The function has to loop thrue the string in a recursive way (= no loops


Sounds like a homework assignment.


Looks like a /bad/ example for learning where to use recursion... ;-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #7
Herfried,
LOL, that too!!!

Jay

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:Oa**************@TK2MSFTNGP15.phx.gbl...
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> schrieb:
The function has to loop thrue the string in a recursive way (= no loops


Sounds like a homework assignment.


Looks like a /bad/ example for learning where to use recursion... ;-).

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Nathon | last post: by
15 posts views Thread by phil-news-nospam | last post: by
12 posts views Thread by AMT2K5 | last post: by
11 posts views Thread by randomtalk | last post: by
15 posts views Thread by ssecorp | last post: by
1 post views Thread by Korara | last post: by
reply views Thread by warner | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.