473,748 Members | 10,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with removing spaces in the string

Hi,

Suppose I have a string like this:

"I have a string \"and a inner string\\\" I want to remove space in
this string but not in the inner string"

In the above string I have to remove spaces, but not in the inner
string(\"and a inner string\\\"). Will anyone please tell me how to do
this?

regards

Sep 19 '07 #1
11 3067
ramu wrote:
Suppose I have a string like this:

"I have a string \"and a inner string\\\" I want to remove
space in this string but not in the inner string"

In the above string I have to remove spaces, but not in the
inner string(\"and a inner string\\\"). Will anyone please
tell me how to do this?
No problem. I suppose you have an input like

char s[] = "I have a string \"and a inner string\\\" I want"
" to remove space in this string but not in the "
"inner string";

Just start removing spaces while you walk through the string;
whenever you see a '"' suspend the space removal and when you
see another '"' resume it. Don't forget to stop at the end of
the string.

Ralf
Sep 19 '07 #2
"ramu" writes:
Suppose I have a string like this:

"I have a string \"and a inner string\\\" I want to remove space in
this string but not in the inner string"

In the above string I have to remove spaces, but not in the inner
string(\"and a inner string\\\"). Will anyone please tell me how to do
this?
Set a flag indicating you have encountered the first delimiter. Think of one
or more _states_. Search for information on modes and states and state
diagrams if you still have trouble.
Sep 19 '07 #3
On Sep 19, 12:16 pm, "osmium" <r124c4u...@com cast.netwrote:
"ramu" writes:
Suppose I have a string like this:
"I have a string \"and a inner string\\\" I want to remove space in
this string but not in the inner string"
In the above string I have to remove spaces, but not in the inner
string(\"and a inner string\\\"). Will anyone please tell me how to do
this?

Set a flag indicating you have encountered the first delimiter. Think of one
or more _states_. Search for information on modes and states and state
diagrams if you still have trouble.
I have done like this:
char *strrmspace (char *s )
{
char *sptr = s;
char *instr = s;
char *outstr;
int i = 0;
boolean found_quote = FALSE;

outstr = (char * )malloc(strlen( s)+1);

while ( *instr != '\0')
{
if ( found_quote )
{
outstr[i++] = *instr;
}
else
{
if ( !isspace( *instr) )
{
outstr[i++] = *instr;
}
}

if (( *instr == '"') && ( instr[-1] != '\\'))
{
found_quote = !found_quote;
}
instr++;
} /* while */
outstr[i] = '\0';
(void)strcpy(s, outstr);
free (outstr);
return(sptr);
} /* strrmspace */

And if I have a string

char s[] = "I have a string \"\\\" I want to remove space in this
string but not in the \"inner string\" ";

But this is removing spaces in \"inner string\" when there is
backslash in the previous inner string ie. \"\\\".
I don't want to remove the spaces in the inner strings. How can I
achieve this?

regards
Sep 19 '07 #4
In article <11************ **********@22g2 000hsm.googlegr oups.com>,
ramu <ra******@gmail .comwrote:
>Suppose I have a string like this:
>"I have a string \"and a inner string\\\" I want to remove space in
this string but not in the inner string"
>In the above string I have to remove spaces, but not in the inner
string(\"and a inner string\\\"). Will anyone please tell me how to do
this?
Incomplete specification.
- Do inner strings nest?
- why is the ending delimeter for the inner string \\\" instead
of \" ? What should happen if a \" appears in the inner string?
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
Sep 19 '07 #5
"ramu" wrote:
I have done like this:
char *strrmspace (char *s )
{
char *sptr = s;
char *instr = s;
char *outstr;
int i = 0;
boolean found_quote = FALSE;

outstr = (char * )malloc(strlen( s)+1);

while ( *instr != '\0')
{
if ( found_quote )
{
outstr[i++] = *instr;
}
else
{
if ( !isspace( *instr) )
{
outstr[i++] = *instr;
}
}

if (( *instr == '"') && ( instr[-1] != '\\'))
{
found_quote = !found_quote;
}
instr++;
} /* while */
outstr[i] = '\0';
(void)strcpy(s, outstr);
free (outstr);
return(sptr);
} /* strrmspace */

And if I have a string

char s[] = "I have a string \"\\\" I want to remove space in this
string but not in the \"inner string\" ";
See, I don't know what the word this applies to. We are not communicating.
Furthermore, I don't *want* to know. You have to have rigorous definitions
to have conversations such as the one you are attempting.
But this is removing spaces in \"inner string\" when there is
backslash in the previous inner string ie. \"\\\".
I don't want to remove the spaces in the inner strings. How can I
achieve this?
The link is not very good but it might help. This is a very simple thing
but it is almost impossible to describe without the ability to make
drawings. You have states and there are certain characters, sometimes
called terminals, that change the state, perhaps the quote or left slash in
your example. Or perhaps both, I didn't try to follow the details of what
you want. You can have an unlimited number of states. There is no
requirement for any kind of symmetry in a state diagram, simple examples
might give you that impression. You might also look up meta-language. I
think you are using quote and left backslash as a meta language in your
posts.
http://en.wikipedia.org/wiki/State_diagram
Sep 19 '07 #6

"ramu" <ra******@gmail .comwrote in message
news:11******** **************@ k79g2000hse.goo glegroups.com.. .
On Sep 19, 12:16 pm, "osmium" <r124c4u...@com cast.netwrote:
>"ramu" writes:
Suppose I have a string like this:
"I have a string \"and a inner string\\\" I want to remove space in
this string but not in the inner string"
In the above string I have to remove spaces, but not in the inner
string(\"and a inner string\\\"). Will anyone please tell me how to do
this?

Set a flag indicating you have encountered the first delimiter. Think of
one
or more _states_. Search for information on modes and states and state
diagrams if you still have trouble.

I have done like this:
char *strrmspace (char *s )
{
char *sptr = s;
char *instr = s;
char *outstr;
int i = 0;
boolean found_quote = FALSE;

outstr = (char * )malloc(strlen( s)+1);

while ( *instr != '\0')
{
if ( found_quote )
{
outstr[i++] = *instr;
}
else
{
if ( !isspace( *instr) )
{
outstr[i++] = *instr;
}
}

if (( *instr == '"') && ( instr[-1] != '\\'))
{
found_quote = !found_quote;
}
instr++;
} /* while */
outstr[i] = '\0';
(void)strcpy(s, outstr);
free (outstr);
return(sptr);
} /* strrmspace */

And if I have a string

char s[] = "I have a string \"\\\" I want to remove space in this
string but not in the \"inner string\" ";

But this is removing spaces in \"inner string\" when there is
backslash in the previous inner string ie. \"\\\".
I don't want to remove the spaces in the inner strings. How can I
achieve this?

regards

When you encounter a quote, check the previous character to see
whether that quote is escaped or not.
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Aero Stability and Controls Computing
Sep 19 '07 #7
On Sep 19, 2:13 pm, rober...@ibd.nr c-cnrc.gc.ca (Walter Roberson)
wrote:
In article <1190217378.900 611.188...@22g2 000hsm.googlegr oups.com>,

ramu <ramu....@gmail .comwrote:
Suppose I have a string like this:
"I have a string \"and a inner string\\\" I want to remove space in
this string but not in the inner string"
In the above string I have to remove spaces, but not in the inner
string(\"and a inner string\\\"). Will anyone please tell me how to do
this?

Incomplete specification.
- Do inner strings nest?
- why is the ending delimeter for the inner string \\\" instead
of \" ? What should happen if a \" appears in the inner string?
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson
No. inner strings do not nest. But inner strings can contain one or
more double quote and/or one or more back slash.
The delimiter is double quote and \\ is the part of the inner
string.

The user input might be like this:

"I have a string "\\" I want to remove space in
this "string but not in the" "\"" inner string"

Regards

Sep 19 '07 #8
On Wed, 19 Sep 2007 08:56:18 -0700, ramu wrote:
Hi,

Suppose I have a string like this:

"I have a string \"and a inner string\\\" I want to remove space in
this string but not in the inner string"

In the above string I have to remove spaces, but not in the inner
string(\"and a inner string\\\"). Will anyone please tell me how to do
this?
Use a status variable, initialized to (e.g. 0). Loop through the
source. If a character is whitespace, copy it only if the status
is 0. If it is a quotation mark, toggle the status, and copy it
to the target. Else, copy it to the target. A switch statement in
a loop will do that.
--
Army1987 (Replace "NOSPAM" with "email")
If you're sending e-mail from a Windows machine, turn off Microsoft's
stupid “Smart Quotes” feature. This is so you'll avoid sprinkling garbage
characters through your mail. -- Eric S. Raymond and Rick Moen

Sep 19 '07 #9
On Wed, 19 Sep 2007 09:53:54 -0700, ramu <ra******@gmail .comwrote:
>On Sep 19, 12:16 pm, "osmium" <r124c4u...@com cast.netwrote:
>"ramu" writes:
Suppose I have a string like this:
"I have a string \"and a inner string\\\" I want to remove space in
this string but not in the inner string"
In the above string I have to remove spaces, but not in the inner
string(\"and a inner string\\\"). Will anyone please tell me how to do
this?

Set a flag indicating you have encountered the first delimiter. Think of one
or more _states_. Search for information on modes and states and state
diagrams if you still have trouble.

I have done like this:
Not a bad first effort.
>char *strrmspace (char *s )
Unfortunately, you have chosen a reserved name for your function.
>{
char *sptr = s;
char *instr = s;
char *outstr;
int i = 0;
boolean found_quote = FALSE;

outstr = (char * )malloc(strlen( s)+1);
Don't cast the return from malloc. It can never help but can lead to
silencing a diagnostic you would really want to see.
>
while ( *instr != '\0')
{
if ( found_quote )
{
outstr[i++] = *instr;
}
else
{
if ( !isspace( *instr) )
isspace returns true for characters other than ' '.
{
outstr[i++] = *instr;
}
}

if (( *instr == '"') && ( instr[-1] != '\\'))
This will cause undefined behavior if a " is the first character of
the input string.
{
found_quote = !found_quote;
}
instr++;
} /* while */
outstr[i] = '\0';
(void)strcpy(s, outstr);
Why the cast?
free (outstr);
return(sptr);
sptr was set to s and neither was ever modified. It looks like you
don't need sptr and can return s. return is a statement, not a
function; the parentheses are unnecessary.
>} /* strrmspace */

And if I have a string

char s[] = "I have a string \"\\\" I want to remove space in this
string but not in the \"inner string\" ";

But this is removing spaces in \"inner string\" when there is
backslash in the previous inner string ie. \"\\\".
I don't want to remove the spaces in the inner strings. How can I
achieve this?
It would avoid ambiguity if you would specify the desired output after
processing the above array and show us the output your function is
producing.

One problem you may think you have is that the \" you see after the
first word string is not two characters at execution time. It is
acutely the start of your inner string which continues until the \"
before the word inner. The phrase inner string is part of your outer
string and should be stripped.
Remove del for email
Sep 20 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
6735
by: hokiegal99 | last post by:
This script works as I expect, except for the last section. I want the last section to actually remove all spaces from the front and/or end of filenames. For example, a file that was named " test " would be renamed "test" (the 2 spaces before and after the filename removed). Any suggestions on how to do this? import os, re, string print " " print "--- Remove '%2f' From Filenames ---" print " "
11
15017
by: gopal srinivasan | last post by:
Hi, I have a text like this - "This is a message containing tabs and white spaces" Now this text contains tabs and white spaces. I want remove the tabs and white spaces(if it more than once between two words). Is there any function we have in C which will find out the tabs and white spaces and returns the text in the follwong way -
12
3492
by: Magix | last post by:
Hi, Everytime I received a fix-length of string, let say 15 (the unused portion will filled with Spaces before receive), I want to remove the Spaces from END until I encounter a non-space char. let say: testBuf is the buffer that receive this fix-length string. printf("",testBuf); Output:
1
1794
by: Mike in Paradise | last post by:
Is there a more effcient way of removing the spaces from the names for a Enumerated value that has several values when you split it)??? When you do a toString it puts ,<SPACE> between the entries eg. category.ToString() = "cat1, cat2, cat3" What I am currently doing..(and I use this a lot..) this.categoriesEntry.Items.AddRange(
3
2829
by: lino | last post by:
Hello, I have the following string: const char plaintext = "Fourscore and seven years ago our \ fathers brought forth upon this continent \ a new nation, conceived in liberty, and dedicated \ to the proposition that all men are created equal.";
4
1605
by: nik | last post by:
hello friends, i want to create a file and copy that file removing white spaces in the file; for ex a file : C:\\abcd.txt contents of file r: a b c d e. i want to remove white spaces from the file using skipws . how can i do this.
8
2507
by: starsky51 | last post by:
I'm sure it's something i'm doing wrong, I just can't see it. I've set up a simple page with the following code: <html> <head> <title>tester</title> <script language="javascript" type="text/javascript"> alert('First String Second String'); </script> </head>
2
6544
by: David | last post by:
Sent this to alt.php a couple of days back, but doesn't look like I'll get an answer, so trying here. I'm trying to convert a script to use friendly URLs, I've done this before, but my PHP skills are quite basic so far, far from proficient at this. ..htaccess file- DirectoryIndex default.php index.asp index.html index.htm index.php
2
10466
code green
by: code green | last post by:
I am trying to write a simple function that will take a string containing an address line or business name and return it nicely formatted. By this I mean extra spaces removed and words capitalised. I also wish it to be legal XML.. The result string is further sent to another function to check the names and addresses don't appear on a blacklist which is why the extra spaces need removing. This is my test function so far function...
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9370
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8242
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6074
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3312
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2782
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.