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

  • Thread starter Thread starter GysAnn
  • Start date Start date
G

GysAnn

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
 
GysAnn said:
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)
 
Oenone said:
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
 
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
 
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
 
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
 
Jay B. Harlow said:
Sounds like a homework assignment.

Looks like a /bad/ example for learning where to use recursion... ;-).
 
Herfried,
LOL, that too!!!

Jay

Herfried K. Wagner said:
Looks like a /bad/ example for learning where to use recursion... ;-).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top