Jump to content

Recommended Posts

Posted

In my application I need to capitalize the first character after each space in a string, is there anything faster than this:

var
mystring:string;

begin
mystring:=stringreplace(mystring, ' a', ' A', [rfReplaceAll]);
mystring:=stringreplace(mystring, ' b', ' B', [rfReplaceAll]);
mystring:=stringreplace(mystring, ' c', ' C', [rfReplaceAll]);
mystring:=stringreplace(mystring, ' d', ' D', [rfReplaceAll]);
mystring:=stringreplace(mystring, ' e', ' E', [rfReplaceAll]);
mystring:=stringreplace(mystring, ' f', ' F', [rfReplaceAll]);
mystring:=stringreplace(mystring, ' g', ' G', [rfReplaceAll]);
mystring:=stringreplace(mystring, ' h', ' H', [rfReplaceAll]);
mystring:=stringreplace(mystring, ' i', ' I', [rfReplaceAll]);
mystring:=stringreplace(mystring, ' j', ' J', [rfReplaceAll]);

Down to z and Z, it works, but it seems like a dirty, or roundabout method, is there any easier way of doing this?


Posted

you could put it in a loop.

depending upon the languae you are using you may be able to do

for x:=a to z
mystring:=stringreplace(mystring, ' ' & x, ' ' & upcase(x), [rfReplaceAll]);
next

or

for x:=97 to 122
mystring:=stringreplace(mystring, ' ' & chr(x), ' ' & chr(x-32), [rfReplaceAll]);
next

Or another optiopn would be to go throught the string on character at a time and when you find a space you replace the next character with uppercase.

Posted
you could split the string using space as a delimiter, capitalise the first letter and then concatenate them back together again

I like that idea. Or does stringreplace allow for wildcards? That be too easy.

Posted (edited)
you could split the string using space as a delimiter, capitalise the first letter and then concatenate them back together again
That is even moar inefficient (string splitting and concatenation involves a huge amount of overhead in most HLLs).

Remember that stringreplace has to make a complete pass over the input string for *every* character you want to replace. Why not uppercase them all at once in one pass?

I shall provide a reasonably efficient method, example is in C.

cap_space(char *s) {
while(*s) {
if((*s==32) && *(s+1)) { /* a space and next will be capitalised if not the terminus */
*(s+1) &= 224;
s++; /* don't reexamine already capitalised letters and spaces */
}
s++;
}
}

Edit: da­mn indents won't stay, isn't that the point of [­code] tags? :angry:

Edited by LLXX
Posted (edited)
you could split the string using space as a delimiter, capitalise the first letter and then concatenate them back together again
That is even moar inefficient (string splitting and concatenation involves a huge amount of overhead in most HLLs).

Remember that stringreplace has to make a complete pass over the input string for *every* character you want to replace. Why not uppercase them all at once in one pass?

i aint a professional coder, i program in vb mainly and the size of the programs i make efficiency isnt really important :P

Edited by ColdFusion200
Posted (edited)

Thanks, I got an idea from your code to use the About Delphi "alphabet puzzle", and hacked it to support renaming the lowercase version of ' x' to the uppercase version, it seems to handle large amounts of usage efficiently :)

EDIT: I 'hacked it to support renaming...', not 'I hack it to...'

Edited by BrainDrain

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...