C --- Remove all extra spaces in string excluding chars between specified characters - c

I have this string: print "Foo cakes are yum"
I need to somehow strip all extra whitespace but leave text between quotes alone. This is what i have so far:
char* clean_strip(char* string)
{
int d = 0, c = 0;
char* newstr;
while(string[c] != '\0'){
if(string[c] == ' '){
int temp = c + 1;
if(string[temp] != '\0'){
while(string[temp] == ' ' && string[temp] != '\0'){
if(string[temp] == ' '){
c++;
}
temp++;
}
}
}
newstr[d] = string[c];
c++;
d++;
}
return newstr;
}
This returns this string: print "Foo cakes are yum"
I need to be able to skip text between thw quotes so i get this: print "Foo cakes are yum".
Here is the same question but for php, i need a c answer: Remove spaces in string, excluding these in specified between specified characters
Please help.

Try this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* clean_strip(char* string)
{
int d = 0, c = 0;
char* newstr = malloc(strlen(string)+1);
int quoted = 0;
while(string[c] != '\0'){
if (string[c] == '"') quoted = !quoted;
if(!quoted && string[c] == ' '){
int temp = c + 1;
if(string[temp] != '\0'){
while(string[temp] == ' ' && string[temp] != '\0'){
if(string[temp] == ' '){
c++;
}
temp++;
}
}
}
newstr[d] = string[c];
c++;
d++;
}
newstr[d] = 0;
return newstr;
}
int main(int argc, char *argv[])
{
char *input = "print \"Foo cakes are yum\"";
char *output = clean_strip(input);
printf(output);
free(output);
return 0;
}
This will produce the output:
print "Foo cakes are yum"
It works by looking for the " character. If it's found it toggles the variable quoted. If quoted is true, then the whitespace removal is skipped.
Also, your original function never allocates memory for newstr. I added the newstr = malloc(...) part. It is important to allocate memory for strings before writing to them.

I simplified your logic a little.
int main(void)
{
char string[] = "print \"Foo cakes are yum\"";
int i = 0, j = 1, quoted=0;
if (string[0] == '"')
{
quoted=1;
}
for(i=1; i< strlen(string); i++)
{
if (string[i] == '"')
{
quoted = 1-quoted;
}
string[j] = string[i];
if (string[j-1]==' ' && string[j] ==' ' && !quoted)
{
;
}
else
{
j++;
}
}
string[j]='\0';
printf("%s\n",string);
return 0;
}

Related

Removing a newline from the middle of a string using getchar

Right now I have a string looking like this:
A sentence
with a newline.
I'm reading the string in via console input like so:
ch = getchar();
while (ch != '.') {
msg[i] = ch;
i++;
ch = getchar();
}
And, after reading it in, I remove the whitespace present by doing this in a function and applying to the msg char array:
char *remove_white_spaces(char *str) {
int i = 0, j = 0;
while (str[i]) {
if (str[i] != ' ')
str[j++] = str[i];
i++;
}
str[j] = '\0';
return str;
}
I've tried looping over it and stopping at \n but that leaves an output of "Asentence", as the string terminates as the \n is set to 0.
Whole main:
int main(void) {
char msg[MAX_MSG_LEN+1];
char ch;
int i = 0;
ch = getchar();
while (ch != '.') {
msg[i] = ch;
i++;
ch = getchar();
}
msg[i] = '.';
msg[i + 1] = '\0';
remove_white_spaces(msg);
printf("%s\n", msg);
return 0;
}
You can use the isspace function to test for and skip any/all whitespace characters, include the normal space and the newline character(s):
#include <ctype.h> // For definition of "isspace"
char *remove_white_spaces(char *str) {
int i = 0, j = 0;
while (str[i]) {
if (!isspace((unsigned char)(str[i])))
str[j++] = str[i];
i++;
}
str[j] = '\0';
return str;
}
On the reason for casting the argument to isspace to an unsigned char, see this discussion.
Function removing and replacing any chars in the string.
toRemove - chars to remove
addSpace - replace with space
allowMultiple - allow multiple spaces when replacing more adjanced
characters
allowEdges - allow adding spaces at the from and at the end
char *removeChars(char *str, const char *toRemove, const int addSpace, const int allowMultiple, int const allowEdges)
{
char *rd = str, *wr = str;
int replaced = 0;
if(rd)
{
while(*rd)
{
if(strchr(toRemove, *rd))
{
if(addSpace)
{
if(!replaced || allowMultiple)
{
if(wr != str || (wr == str && allowEdges))
{
*wr++ = ' ';
replaced = 1;
}
}
}
}
else
{
*wr++ = *rd;
replaced = 0;
}
rd++;
}
if(allowEdges) *wr = 0;
else
while((wr - 1) > str)
{
if(*(wr - 1) == ' ') {*(wr - 1) = 0; wr--;}
else break;
}
}
return str;
}
int main(void)
{
char str[] = "%%%%%A sentence\n\n\nwith!##$%^a newline.%%%%%%%";
printf("`%s`\n", removeChars(str,"\n!##$%^", 1, 0, 0));
}
Following the suggestion of #MarkBenningfield I did the following and checked for '\n' and just replaced it with a space.
while (ch != '.') {
msg[i] = ch;
i++;
ch = getchar();
if (ch == '\n') {
ch = ' ';
}
}

removing spaces from a string in C language

I can't figure out how to remove the spaces at the beginning of the sentence without using any libraries other than stdio.h and stdlib.h.
#include <stdio.h>
int main()
{
char text[1000], result[1000];
int c = 0, d = 0;
printf("Enter some text\n");
gets(text);
while (text[c] != '\0') { // till the end of the string
if (text[c] == ' ') {
int temp = c + 1;
if (text[temp] != '\0') {
while (text[temp] == ' ' && text[temp] != '\0') {
if (text[temp] == ' ') {
c++;
}
temp++;
}
}
}
result[d] = text[c];
c++;
d++;
}
result[d] = '\0';
printf("Text after removing blanks\n%s\n", result);
return 0;
}
This piece of code removes all the extra spaces of a sentence.
Example:
input: " this is my program."
output: " this is my program."
EXPECTED OUTPUT: "this is my program."
this code leaves only one space where there were more spaces, but I want to remove all spaces at the beginning as well like in the expected output.
#include <stdio.h>
int main()
{
char text[1000], result[1000];
int c = 0, d = 0;
printf("Enter some text\n");
gets(text);
// no space at beginning
while(text[c] ==' ') { c++; }
while(text[c] != '\0'){
result[d++] = text[c++]; //take non-space characters
if(text[c]==' ') { result[d++] = text[c++]; } // take one space between words
while(text[c]==' ') { c++; } // skip other spaces
}
result[d] = '\0';
printf("Text after removing blanks\n%s\n", result);
return 0;
}
I want to remove all spaces at the beginning as well.
After getting the input, begin processing up to the first non-white-space.
// do not use gets()
fgets(text, sizeof text, stdin);
text[strcspn(text, "\n")] = '\0'; //lop off potential \n
char *ptext = text;
while (isspace((unsigned char) *ptext)) {
ptext++;
}
// now use ptext instead of text for rest of code.
This function will process the input array as you need
void remove_white_space(char *source, char *result) {
int i=0,key=0,k=0;
while (source[i]!='\0') {
if(source[i]==' ') {
if (key== 0) {
if(i==0) {
key=1;
++i;
} else {
key=1;
result[k]=source[i];
++k;
++i;
}
} else
++i;
} else {
key=0;
result[k]=source[i];
++k;
++i;
}
}
result[k]='\0';
}

How to Tokenize string[array]?

I need to tokenize a string from an array, i need just three words and ignore all tabs '\t' and spaces ' '
the array line[] is just a test case.
I debugged mine, the first array (supposed to carry only the first word) got filled by spaces & letters from 3 words, not stopping after the first word when a tab or space is met. BTW my program crashed. i suspect getting out of array bounds maybe.
What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char line[] = " CLOOP LDA buffer,x";
char array1[20] ="";
char array2[20] ="";
char array3[20] ="";
int i = 0;
for( i ; i<strlen(line) ; i++)
{
while ( (line[i] != ' ') && (line[i] != '\t'))
{
if(array1[0] == '\0')
{
int j = 0;
while(line[i] != ' ' && line[i] != '\t')
{
array1[j] = line[i];
i++;
j++;
}
}
if(array2[0] =='\0');
{
int k = 0;
while(line[i] != ' ' && line[i] != '\t')
{
array2[k] = line[i];
i++;
k++;
}
}
if(array3[0] == '\0')
{
int g = 0;
while(line[i] != ' ' && line[i] != '\t')
{
array3[g] = line[i];
i++;
g++;
}
}
}
}
printf("array 1: %s\n array2: %s\n array3: %s\n", array1, array2, array3);
return(0);
}
You are over-complicating things. First of all it is difficult to feed all 3 arrays at the same time. The processing for one token should be completely finished before moving to the other token.
I would propose to "eat" all the white spaces before starting to process a token.
That is done by:
// skip leading delimiters
if( skip_leading_delimiters )
{
if( is_delimiter( delimiters, line[i]) ) continue;
skip_leading_delimiters = 0;
}
After token is processes you can move to the next token and process it. I tried to preserve your concept and approach as much as I could. The amount of while loops has been reduced to 0 since // skip leading delimiters section takes care of it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int is_delimiter(const char * delimiters, char c) // check for a delimiter
{
char *p = strchr (delimiters, c); // if not NULL c is separator
if (p) return 1; // delimeter
else return 0; // not a delimeter
}
int main()
{
char line[] = " CLOOP LDA buffer,x";
char array1[20];
char array2[20];
char array3[20];
int con1 = 1;
int con2 = 0;
int con3 = 0;
int con1s = 0;
int con2s = 0;
int con3s = 0;
int i = 0;
int j = 0;
int skip_leading_delimiters = 1;
char * delimiters = " \b";
for(i = 0; i < strlen(line); i++)
{
// skip leading delimiters
if( skip_leading_delimiters )
{
if( is_delimiter( delimiters, line[i]) ) continue;
skip_leading_delimiters = 0;
}
if(con1)
{
if(line[i] != ' ' && line[i] != '\t')
{
array1[j] = line[i];
j++;
array1[j] = 0;
con1s = 1;
}
else
{
if(con1s)
{
con1 = 0;
con2 = 1;
skip_leading_delimiters = 1;
j = 0;
}
continue;
}
}
if(con2)
{
if(line[i] != ' ' && line[i] != '\t')
{
array2[j] = line[i];
j++;
array2[j] = 0;
con2s = 1;
}
else
{
con2 = 0;
con3 = 1;
skip_leading_delimiters = 1;
j = 0;
continue;
}
}
if(con3)
{
if(line[i] != ' ' && line[i] != '\t')
{
array3[j] = line[i];
j++;
array3[j] = 0;
con3s = 1;
}
else
{
con3 = 0;
j = 0;
continue;
}
}
}
printf(" array1: %s\n array2: %s\n array3: %s\n", array1, array2, array3);
return(0);
}
Output:
array1: CLOOP
array2: LDA
array3: buffer,x

Copying contents from one array to another in c

Here is an outline of what my program is suppose to do so far.
While there are more words in inputLine:
take next word from the inputLine
if next word fits in the inputline2
add the nextword to inputline2 (doesn't work and maybe not needed)
add the inputline2 to outputBuffer (doesn't work)
format outputBuffer
Otherwise:
write the outputBuffer to the output file
empty out the outputBuffer (put \0 in position 0)
No matter what I try though, the outputline2 and/or outputBuffer never copy the contents of the inputline properly. The only reason I have inputline2 is because I was originally using fgets and putting the contents from a line in a text file into inputline. However, since my array length is suppose to be 40, it would always cut some of the words in the original line in half. If this could be avoided somehow I wouldn't even need inputline2. Either way, in both cases, the contents from word (which is just a single word from the original inputline) won't ever copy properly.
void format(FILE *ipf, FILE *outf)
{
char inputline[80];
char outputBuffer[MaxOutputLine];
char word[MaxOutputLine];
while(fgets(inputline, 80, ipf) != NULL)
{
int pos = 0;
int i;
int j = 0;
char inputline2[MaxOutputLine] = {'\0'};
while(pos != -1)
{
i=0;
pos = nextword(inputline, word, pos);
if(strlen(word) <= (40 - strlen(inputline2)))
{
while(i < strlen(word))
{
inputline2[j] = word[i];
i++;
j++;
}
j++;
printf("%s", inputline2);
}
}
}
}
int nextword(char *inputline, char *word, int pos1) //takes a word beginning from pos and puts it in word and re\
turns the new position of beginning of the next word
{
int pos2 = 0;
if(inputline[pos1] == '\0')
{
return -1;
}
if(inputline[pos1] == ' ')
{
while(inputline[pos1] == ' ')
{
pos1++;
}
}
else
{
while(inputline[pos1] != ' ' && inputline[pos1] != '\n' && inputline[pos1] != '\0')
{
word[pos2] = inputline[pos1];
pos1++;
pos2++;
}
if(pos1 == '\n' || pos1 == '\0')
{
return -1;
}
pos1++;
}
word[pos2]='\0';
return pos1;
}
As I can't format a code in comment I send it as answer.
First, I guess your nextword() is similar to:
#include <stdio.h>
#include <string.h>
int nextword(char *inputline, char *word, int pos)
{
char *ptr;
int len;
ptr = strchr(&inputline[pos], ' ');
len = ptr ? ptr - &inputline[pos] : strlen(&inputline[pos]);
strncpy(word, &inputline[pos], len);
word[len] = 0;
if (ptr == NULL)
return -1;
return pos + len + 1;
}
To make clear:
#define MaxOutputLine 40
In format() you have missed the line:
if(strlen(word) <= (40 - strlen(inputline2)))
{
while(i < strlen(word))
{
inputline2[j] = word[i];
i++;
j++;
}
inputline2[j] = ' ';/*missed, otherwise undefined, eg. \0 */
j++;
printf("%s", inputline2);
}
Some compilers have problem with long buffers on stack. You can try to add "static" before char buf[] in format().
In nextword() you have the mistake:
if(pos1 == '\n' || pos1 == '\0')
{
return -1;
}
instead of
if(inputline[pos1] == '\n' || inputline[pos1] == '\0')
{
word[pos2]='\0';
return -1;
}
In my opinion nextword() could be simplified to something like that:
int nextword(char *inputline, char *word, int pos1)
{
int pos2 = 0;
while(inputline[pos1] != ' ' && inputline[pos1] != '\n' && inputline[pos1] != '\0')
{
word[pos2] = inputline[pos1];
pos1++;
pos2++;
}
word[pos2]='\0';
if(inputline[pos1] == '\n' || inputline[pos1] == '\0')
{
return -1;
}
return pos1 + 1;
}

Change more than one space in between words into one

I need to read the text and find if there is more than one space between the words.
If there is change it to one.
For example if I have a text:
My name is Lukas
Program should change it to:
My name is Lukas
Any ideas?
while (*str) {
if (*str != ' ' || str[1] != ' ') *newstr++ = *str;
str++;
}
*newstr = 0;
j = 0;
for(i=0; myStr[i] != '\0'; i++) {
if(myStr[i] == ' ' && myStr[i+1] == ' ')
continue;
newStr[j] = myStr[i];
j++;
}
And don't forget to add '\0' (Which indicates the end of the string) to the end of newStr
isspace from <ctype.h> could be useful here. Here is a possible implementation:
void delete_multiple_spaces(char *dst, const char *src)
{
int previous = 0;
int c;
while ((c = *src++) != '\0')
{
if (isspace(c) && !previous)
{
previous = 1;
}
else
{
if (previous)
{
*dst++ = ' ';
previous = 0;
}
*dst++ = c;
}
}
*dst = '\0';
}
From your earlier query, I modified the logic to fit your requirement. Hope this helps.
FILE *in;
char ch,str[100],cw;
int j,i = 0;
int isSpace = 0;
in=fopen("duom.txt","r");
if(in){
while(!feof(in)){
ch=getc(in);
if(isSpace)
isSpace = (isSpace & (ch == ' '));
if(!isSpace) {
str[i] = ch;
i++;
}
if(ch == ' ')
isSpace = 1;
}
for(j=0;j<i;j++){
printf("%c",str[j]);
}
char *(strdupcompact) (const char *c)
{
int i; int p;
for (i = 0, p = 0; c[i]; i++, p++)
{
if (c[i] == ' ') while (c[i+1] == ' ') i++;
}
char *newstr = malloc(p + 1);
for (i = 0, p = 0; c[i]; i++, p++)
{
newstr[p] = c[i];
if (c[i] == ' ') while (c[i+1] == ' ') i++;
}
newstr[p] = 0;
return newstr;
}
Makes a malloced copy of your string.

Resources