11 Replies - 669 Views - Last Post: 15 March 2009 - 05:53 AM Rate Topic: -----

#1 deghert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 09

Removing space from string

Posted 01 March 2009 - 01:33 PM

Hello I am trying to write a program that removes spaces before and after and reduce to one between words and capitalize the first letter of each world and the other letters lowercase but I'm getting errors which I can't make heads and tail of.
# include <iostream>
# include <cstring>
using namespace std;
void formatering ( char *str, char *tempstr, char *string);
int main()
{
char tempstr[80];
 char str[80]; 
 cin >> str; 
gets (str); 
formatering (str, tempstr, string);
return 0;
}
void formatering ( char *str, char *tempstr, char *string) //unsigned
{
	int a = 0;
	int b = 0;
while (*str != '\0')
	{
if ( *str == ' ' && a == 1 && b == 1)
		{ tempstr = *str;
		b = 0;
	}
	else if ( a == 0 && 'A'<=*str <='Z'  ||'a'<=*str <='z' )
	{
		tempstr  = toupper(*str);
		a = 1;
		b = 1;}
		else if ( a == 1 && 'A'<=*str <='Z'  ||'a'<=*str <='z' )
{
*str = tolower(*str + 1);
}
str++;
string = (string + tempstr); 
}
tempstr--;
if (*str = ' ') {
   *tempstr--;}
   *tempstr++;
   *tempstr = '\0';
cout << string;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Removing space from string

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Removing space from string

Posted 01 March 2009 - 02:31 PM

char str[80];
 cin >> str;
gets (str);


You're mixing C and C++ input. Get rid of the gets() call (permanently...forget you even know about it and look into fgets()) and use the cin>> call.

You can't do this:
'A'<=*str <='Z'


It must be
if (a == 0 && 
    (*str >= 'A' && *str <='Z') || (*str >= 'a' && *str <='z'))
{
    ...
}

Was This Post Helpful? 0
  • +
  • -

#3 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: Removing space from string

Posted 01 March 2009 - 02:51 PM

This should help. Look under the removeSpaces function. I'll try to add a word capitalizing function soon!
Was This Post Helpful? 0
  • +
  • -

#4 deghert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 09

Re: Removing space from string

Posted 01 March 2009 - 04:39 PM

Thank you :D but I still have problem I get following errors:
|12|error: expected primary-expression before ')' token|
22|error: invalid conversion from `char' to `char*'|
|27|error: invalid conversion from `int' to `char*'|
|35|error: invalid operands of types `char*' and `char*' to binary `operator+'|
Was This Post Helpful? 0
  • +
  • -

#5 deghert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 09

Re: Removing space from string

Posted 01 March 2009 - 06:30 PM

I have managed to solve all of them except the one on line 12: formatering (str, tempstr, string); with the error: error: expected primary-expression before ')' token| help would be appreciated :^:
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6064
  • View blog
  • Posts: 23,520
  • Joined: 23-August 08

Re: Removing space from string

Posted 01 March 2009 - 07:46 PM

Name your third variable something other than "string," as that's a reserved word.
Was This Post Helpful? 0
  • +
  • -

#7 deghert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 09

Re: Removing space from string

Posted 02 March 2009 - 08:56 AM

The code runs now but all I get is complete randomness which most likely is caused by misuse of pointer somewhere
# include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
int formatering ( char *str, char *tempstr, char *astring);
int main()
{
	char tempstr[80];
	char str[80];
	char astring[80];
	cin >> str;
	gets (str);
	formatering (str,tempstr,astring );
	return 0;
}
int formatering ( char *str, char *tempstr, char *astring)
{
	int a = 0;//tillfälliga variabler
	int b = 0;
	while (*str != '\0')
	{
		if  ( *str == ' ' && a == 1 && b == 1) //första space efter ett namn
		{
			*tempstr = *str;
			b = 0;
			//a = *str; valid
		}
		else if (a == 0 &&  (*str >= 'A' && *str <='Z') || (*str >= 'a' && *str <='z')) //första bokstaven i namnet
		{
			*tempstr  = toupper(*str);
			a = 1;
			b = 1;
		}
		else if (a == 1 &&  (*str >= 'A' && *str <='Z') || (*str >= 'a' && *str <='z'))//övriga bokstäver
		{
			*str = tolower(*str);
		}
		str++;
		strcat (astring, tempstr); //skapar en sträng som är correct
	tempstr = '\0';
	}
	tempstr--;
	if (*str = ' ')
	{	
		*tempstr--;}
		*tempstr++;
		*tempstr = '\0';
		cout << astring;
	 }


Haven't looked into fgets() yet as I prefer to leave parts that work until the code runs as it should and once again thanks.

This post has been edited by deghert: 02 March 2009 - 09:02 AM

Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3102
  • View blog
  • Posts: 19,142
  • Joined: 14-September 07

Re: Removing space from string

Posted 02 March 2009 - 09:13 AM

Why are you doing this?

cin >> str;
	gets (str);




cin will get one character, but then you do input again...
Was This Post Helpful? 0
  • +
  • -

#9 deghert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 09

Re: Removing space from string

Posted 03 March 2009 - 07:43 AM

Actually I thought i had deleted cin >> str; and since it didn't course any error I must have over looked it but it is now removed but I am still having problem with pointers though.
Was This Post Helpful? 0
  • +
  • -

#10 deghert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 09

Re: Removing space from string

Posted 14 March 2009 - 02:12 PM

Sorry for reviving an old thread but I still can't get it to work. I made comments where I got error. Same error at all three spots and same cause. would be nice if someone could tell me how to fix it. ;)
# include <iostream>
#include <cstring>
using namespace std;
int main()
{
   char stl[80];
   char tempstl[80];
   char originalstl[80];
   cin.getline (originalstl,80);
   char *s;
  s = &originalstl[0];
  while (s!='\0')
  {
	 int a = 0;
	 int b = 1;
   if (*s ==' ')
   {
	  b = 1;
   }
  else if(a == 0 &&  (*s >= 'A' && *s <='Z') || (*s >= 'a' && *s <='z') && b == 1)
  {
	tempstl = toupper (*s); // error here
	b = 0;
	a = 1;
   strcat(tempstl, stl);
  }
  else if(a == 1 &&  (*s >= 'A' && *s <='Z') || (*s >= 'a' && *s <='z') && b == 0)
 {
	tempstl = tolower(*s);  // error here
	strcat(tempstl, stl); 
  }
  else if(a == 1 &&  (*s >= 'A' && *s <='Z') || (*s >= 'a' && *s <='z') && b == 1)
  {
	tempstl = toupper(*s);  // error here
   b = 0;
  strcat(tempstl, stl);
 }
 s++;
 }
return 0;
}


Was This Post Helpful? 0
  • +
  • -

#11 MikeAbyss  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 11
  • View blog
  • Posts: 56
  • Joined: 09-July 07

Re: Removing space from string

Posted 14 March 2009 - 03:38 PM

tempstl = tolower(*s);



tolower returns one charcter and you're technically trying to assign tempstl which contains 80 characters to one character

You need to give it a subscript such as
tempstl[0] =  tolower(*s)


Was This Post Helpful? 0
  • +
  • -

#12 deghert  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-March 09

Re: Removing space from string

Posted 15 March 2009 - 05:53 AM

Thank you the program is now working. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1