Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Nodger

2 straightforward questions

This topic is 5479 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

1. is there any standard c functions for converting strings to upper/lower case? (NOT using classes) 2. how would I write a function that checks a string to see if it has any more than 1 space side by side and if so shortens it so that all spaces are only 1 char long. for example: the string "i like eggs" would become "i like eggs" thx in advance for any hlp

Share this post


Link to post
Share on other sites
Advertisement
Regarding question 2 simply parse the string char by char keeping track of the previous char. If both the previous char and the current char is a space then ignore the char otherwise add it to a destination string. You could either initialize the previous char variable to '\0' or space depending if you wanna strip all left spaces or not.

[edited by - z80 on September 23, 2003 7:11:37 PM]

Share this post


Link to post
Share on other sites
This function should work


char *RemoveSpaces(char *str)
{
char *ret;
int len;
bool spacefound;
int retpos, i;

len = strlen(str);
ret = new char[len];

spacefound = false;
retpos = 0;
for(i=0; i<len; i++)
{
if(spacefound)
{
if(str[i] == ' ')
continue;
else
{
spacefound = false;
ret[retpos] = str[i];
retpos++;
}
}
else
{
if(str[i] == ' ')
spacefound = true;
ret[retpos++] = str[i];
}
}
ret[retpos]='\0';

return ret;
}

I'm sure it can be improved so it will be faster

[edited by - emilbbb on September 23, 2003 7:13:13 PM]

Share this post


Link to post
Share on other sites
Q2.
Bit brute force, but:

send your string to the function.
create a temporary string (tempString)
set a 'spaceFound' flag to false
for each character in string
if character is a space
if spaceFound is false
copy character to tempString
set spaceFound to true
else
copy character to tempString
copy tempString to string


EDIT: beaten to it



[edited by - tuita on September 23, 2003 7:16:48 PM]

Share this post


Link to post
Share on other sites
How about this one?

void RemoveSpaces(char *str)
{
unsigned int i;
char *dest = str + 1;
for (i = 1; i < strlen(str); i++)
if (str[i - 1] != ' ' || str[i] != ' ')
*(dest++) = str[i];
if (str[0] != '\0')
*dest = '\0';
}


[EDIT]: Added "if (str[0] != '\0')" to fix empty-string bug

[edited by - z80 on September 23, 2003 7:49:45 PM]

Share this post


Link to post
Share on other sites
Or faster (and more cryptic) alternative:

void RemoveSpaces(char *str)
{
char *src = str;
char *dest = str + 1;
while (*src != ''\0'')
if (*(src++) != '' '' || *src != '' '')
*(dest++) = *src;
if (*str != ''\0'')
*dest = ''\0'';
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

#!/usr/bin/ruby
STDIN.gets.strip.split(/ +/).join('' '')

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!