Avatar of nortonxe
nortonxeFlag for United States of America

asked on 

clean string

i am writting a function which:

+ reduce all multiple occurences of whitespace characters to a single space character
+ The whitespace characters include newline, horizontal tab, form feed, vertical tab and space characters.

my code is not work

This is correct example:
String to be cleaned :   i             am             a            (tab)      newbie!
Cleaned string       : i am a newbie!!
cout<<"  String to be cleaned: ";
	fflush(stdin);
	cin.getline(s, Size);
	text = s;
 
    for (int i = 0; i < strlen(s); i++)// entire string 
	{
		
		if ((text[i] == ' ')||((int)text[i] == 9))
		{
			if((i = text.find(' ')) != string::npos) 
			{
				text.erase(i, 1);
				
				
			}
			
			if((i = text.find('\t')) != string::npos) 
			{
				text.erase(i,1);
				text.insert(i,1, ' ');
				
			}
		}
		
			
	}
	cout<<" Cleaned string: "<<text<<endl<<endl;

Open in new window

C++

Avatar of undefined
Last Comment
Infinity08
Avatar of Infinity08
Infinity08
Flag of Belgium image

How is s declared ? (and if applicable, how is it allocated ?) Same for text.
Avatar of Infinity08
Infinity08
Flag of Belgium image

Take a look at isspace to make your code shorter, nicer, and make it work :

        http://www.cplusplus.com/reference/clibrary/cctype/isspace.html
Avatar of nortonxe
nortonxe
Flag of United States of America image

ASKER

How is s declared ? (and if applicable, how is it allocated ?) Same for text.

==> sorry, this is full code

void cleanText()
{
char s[Size];
	string text;
 
	cout<<"String to be cleaned: ";
	fflush(stdin);
	cin.getline(s, Size);
	text = s;
 
    for (int i = 0; i < strlen(s); i++)// entire string 
	{
		
		if ((text[i] == ' ')||((int)text[i] == 9))
		{
			if((i = text.find(' ')) != string::npos) 
			{
				text.erase(i, 1);
				
				
			}
			
			if((i = text.find('\t')) != string::npos) 
			{
				text.erase(i,1);
				text.insert(i,1, ' ');
				
			}
		}
		
			
	}
	cout<<" Cleaned string: "<<text<<endl<<endl;
}

Open in new window

Avatar of nortonxe
nortonxe
Flag of United States of America image

ASKER

the cleaned string is the same text
Avatar of Infinity08
Infinity08
Flag of Belgium image

Did you look into isspace ? It will make your code a lot shorter and cleaner.

Currently your code doesn't really do what it's supposed to ;)
Avatar of nortonxe
nortonxe
Flag of United States of America image

ASKER

oh no
after detect space the code could drop excessive space.

exam:
The string: "Hello                       world!"

have more than one space between "hello" and "world", so i want drop down one space only

==> the result: "hello world"
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Infinity08
Infinity08
Flag of Belgium image

May I ask you why you gave a B grade ? That usually means that something was missing in the answer and/or that something is still unclear. If you need further assistance, then you don't have to close the question yet, and you can just ask for clarification where needed.
Avatar of nortonxe
nortonxe
Flag of United States of America image

ASKER

your suggestion made my code shorter
however my problem is not detect whitespace, i want to remove multiple whitespace to simple space

Here is my string:
            A     B                 (C)            D    

I have problems deleting whitespace (space and tabs) from that string afterwards.
The resulting string should be:

A B C D

maybe your solution is good but at presnet my code is not work with isspace()
void cleanText()
{
start:
	system("cls");
	char s[Size];
	string text;
 
	cout<<"String to be cleaned: ";
	fflush(stdin);
	cin.getline(s, Size);
	text = s;
	
    for (int i = 0; i < strlen(s); i++)// entire string 
	{
		if (isspace(text[i]))
		{
			if((i = text.find(' ')) != string::npos) 
			{
                                  text.erase(i, 1);
                        }
			
			if((i = text.find('\t')) != string::npos) 
			{
				text.erase(i,1);
				text.insert(i,1, ' ');
				
			}
			
		}
			
	}
 
	cout<<" Cleaned string: "<<text<<endl<<endl;
}

Open in new window

ee.jpg
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> i want to remove multiple whitespace to simple space

I know, that's why I said this in my previous post :

>> Note that once you find a space, you simply place one ' ' in the target location, and then have a loop that ignores all the spaces following it.

Did you understand what I meant by that ?



Btw, you only replaced one comparison using isspace. You're still comparing to ' ' and '\t' elsewhere.
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> but at presnet my code is not work with isspace()

That has nothing to do with isspace ;) Your current code simply erases all spaces. You should keep one, and erase all the others that immediately follow it.
Avatar of nortonxe
nortonxe
Flag of United States of America image

ASKER

would you help me by rewrit this code, i can compare and learn more about you

thanks
Avatar of Infinity08
Infinity08
Flag of Belgium image

I assume that this is for an assignment, is that right ?
Avatar of nortonxe
nortonxe
Flag of United States of America image

ASKER

for a game.
i and my friend are leanring C++ and challenge oursefl which shortest solution to remove whitespace in string.

i understood your suggestion but i felt tired and want to learn the best code from you. I hope this is the best to compare the way i think and you think.At the end i learn more from it.
Avatar of Infinity08
Infinity08
Flag of Belgium image

Ok. Below is how I would do it. If anything is unclear, do not hesitate to ask.

Fyi : you can ask to get this question re-opened to re-grade it using the "Request Attention" link.
void cleanSpaces(char *str) {
  const char *strGet = str;
  char *strSet = str;
  while (*strGet) {
    if (isspace(*strGet)) {
      *strSet++ = ' ';
      while (isspace(*(++strGet)));
    }
    else {
      *strSet++ = *strGet++;
    }
  }
  *strSet = '\0';
}

Open in new window

Avatar of nortonxe
nortonxe
Flag of United States of America image

ASKER

thanks
i requested re-opened and would re-grade as soon as possible

thanks again for helping me, master ^_^
Avatar of Infinity08
Infinity08
Flag of Belgium image

>> thanks again for helping me

No problem :) If you have any further questions about this, do not hesitate to ask.


>> And Infinity08 is hardly a "master". :-)

Heh ... I see myself as a student at EE university. I'm learning here every day, and being able to transfer some of that knowledge to others in the process is a nice bonus. I love this site ;)
C++
C++

C++ is an intermediate-level general-purpose programming language, not to be confused with C or C#. It was developed as a set of extensions to the C programming language to improve type-safety and add support for automatic resource management, object-orientation, generic programming, and exception handling, among other features.

58K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo