Reputation: 1267
Suppose I have a string that may look something like this:
"value" "some other value" "other value" "some value"
My goal is to remove the blanks selectively, like so:
"value""some other value""other value""some value"
such that the blanks remain only inside strings contained in quotes:
"some other value"
I have the following function:
void rmChar(char *str, char c)
{
char *src, *dest;
src = dest = str;
while(*src != '\0')
{
if (*src != c)
{
*dest = *src;
dest++;
}
src++;
}
*dest = '\0';
}
which removes all occurrences of char c in str and I though I should use some more conditional expressions to do the removal only when certain things happen.
Got any clues?
Upvotes: 0
Views: 105
Reputation: 2720
I just thought of doing this. Below is my program.
Note: This may not be an efficient program (bad time or space complexity), however it does what you are trying to do (if I understood your question right).
Note Also I have used malloc() in this code. You would not use it if you were changing the contents of the original string without using any other string. But as I understood from your question you were making a NEW string which contained the value of original string after removing the spaces.
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void rmChar(char *,char, int );
int main()
{
char string[200] = "\"This is a value\" \"and another value\" \"value2 this\"";
char c;
c = '"';
printf("%s\n",string);
int len = strlen(string);
/*Pass the address of the stringi, char c, and the length of the string*/
/*Length of the string will be required for malloc() inside function rmChar()*/
rmChar(string, c, len);
return 0;
}
void rmChar(char *str,char c, int len)
{
char *dest1, *dest2;
char *src = str;
int removeFlag = 0; /* You will remove all the spaces ' ' that come after removeFlag is odd*/
dest1 = malloc(len);
dest2 = dest1;
while(*str != '\0')
{
if(*str == c)
{
removeFlag++;
if (removeFlag %2 == 0)
{
/* This is required because every 2nd time you get a " removeFlag is increased so next if is NOT true*/
*dest2 = *str;
dest2++;
}
}
if ((removeFlag % 2) == 1)
{
*dest2 = *str;
dest2++;
}
str++;
}
*dest2 = '\0';
printf("%s\n", dest1);
/* If you want to copy the string without spaces to the original string uncomment below line*/
//strcpy(src, dest1);
free(dest1);
}
You needed one more variable to use as some kind of flag which indicated after which " you need to remove spaces. Then you would use that flag in a if()
statement in some way. Here int removeFlag
is the flag I have used.
Upvotes: 1
Reputation: 229593
The loop that iterates over the string has to keep track if it is currently looking at a character inside a quoted string or not, and then use that information to only delete when appropriate.
To keep track of that information you could use an additional variable that gets updated every time there is a "
.
int quoted = 0;
while (...) {
if (*src == '"') {
// set `quoted` to 1 or 0, as appropriate
...
}
// delete only if !quoted
...
}
Upvotes: 1