Change more than one space in between words into one - c

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.

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 = ' ';
}
}

Function and strings

I'm new to C and have started learning about strings. I want to create a function called
void SpaceRemover(char *input )
It should remove the spaced from a given string array that has lots of space
The code that I've produced so far removes all the spaces and doesn't provide the output I'm looking for. Can anyone help me with this?
char* SpaceRemover(char *input){
char *output=input;
for (int i = 0, j = 0; i<strlen(input); i++,j++)
{
if (input[i]!=' ')
output[j]=input[i];
else
j--;
}
return output;
}
I made this but I know its wrong and does not do what i want it to but honestly this is all i could this of -_-
You could just do as below
void SpaceRemover(char *name)
{
int i=0,j=0;
for (i = 0;i<strlen(name);i++)
{
if (name[i] != ' ' || (name[i] == ' ' && name[i+1] != ' ' && j!= 0))
{
name[j++] = name[i];
}
}
name[j]='\0'; //Terminate the string to avoid junk chars
}
Where
if (name[i] != ' ' || (name[i] == ' ' && name[i+1] != ' ' && j != 0))
will let you copy only if current char is not space or current char is space and next char is not space(to include single space apart in the beginning).
Also don't forget to terminate the string.
name[j]='\0';
The problem is that you remove all spaces.
char *SpaceRemover(char *name){
char *output = name;
int j = 0;
for (int i = 0; i < strlen(name); i++) {
if (name[i] != ' ' || (name[i] == ' ' && name[i + 1] != ' ') {
output[j] = name[i];
j += 1;
}
}
output[j] = '\0'
return output;
}
This condition should let one space through.
You might notice I replaced the void return type with a char * so t o use the function you will need to use:
name = SpaceRemover(name);
I would be using a flag to activate when a space is met.
This might need to be tweaked if you want to remove leading and trailing spaces too.
A space will be added to output and the flag will be used to avoid the next ones to be added. The flag will be deactivated when something else than a space is met.
As stated Alex in comments, decrementing j in loop while it's incremented in the for statement isn't recommended.
I would copy each characters in the for loop instead of filtering a pre-copied output.
char space_found = 0;
char *output = malloc(sizeof(char) * (strlen(name) + 1));
int j = 0;
for (int i = 0; i < strlen(name); ++i)
{
if (name[i] == ' ' and space_found == 0)
{
space_found = 1;
output[j++] = name[i];
}
if (name[i] != ' ')
{
space_found = 0;
output[j++] = name[i];
}
}
output[j] = '\0';
void spaceRemover(char* str)
{
char temp[50] = {0};
int j = 0;
strncpy(temp, str, strlen(str) + 1);
for(int i = 0; i < strlen(str); i++)
{
if(temp[i] != ' ')
{
str[j] = temp[i];
j++;
}
}
str[j] = 0;
}
if you have any questions, feel free to ask, Good Luck
#include <stdio.h>
char *rem(char *str)
{
char *cur=str;
char *nex=str;
while(*nex)
{
if(*nex == ' ') nex++;
else *cur++ = *nex++;
}
*cur=0;
return str;
}
int main(void) {
char z[]=" etc def etc def ";
printf("%s\n", rem(z));
return 0;
}

Splitting word by specific char work with string but don't work with argv[1]

I have an issue with my program.
My program split word using \t \n or space and put the word into a array of string.
when i call my function like this its works perfectly :
ft_print_words_tables(ft_split_whitespaces("Hello Everyone this is a test"));
but when i try to send the first command line param like this :
ft_print_words_tables(ft_split_whitespaces(argv[1]));
i'm getting the following error :
./a.out "test test tast"
a.out(97132,0x7fff706ff000) malloc: * error for object 0x7f9e09403228: incorrect checksum for freed object - object was probably modified after being freed.
* set a breakpoint in malloc_error_break to debug
[1] 97132 abort ./a.out "test test tast"
here is the code :
#include <stdlib.h>
// This func return the word nbr
int ft_compte_mot(char *str) {
int i = 0;
int j = 0;
while(str[i] == ' ' || str[i] == '\t' || str[i] == '\n') {
i++;
while (str[i] != '\0') {
if ((str[i] == ' ' || str[i] == '\n' || str[i] == '\t') &&
(str[i + 1] >= '!' && str[i + 1] <= 'z')) {
j++;
}
i++;
}
return (j + 1);
}
// this func count the word lenght and put them in an int array
int *ft_compte_taille_mot(int *taillemot, char *str) {
int i = 0;
int j = 0;
int k = 0;
while (str[i] != '\0') {
j = 0;
while ((str[i] == ' ' || str[i] == '\n' || str[i] == '\t')
&& str[i] != '\0')
i++;
while (str[i] != ' ' && str[i] != '\n' && str[i] != '\t'
&& str[i] != '\0') {
j++;
i++;
}
taillemot[k] = j;
i++;
k++;
}
return (taillemot);
}
void ft_copy_word(int *taillemot, int nbmot, char **tab, char *str) {
int i = 0;
int j = 0;
int k = 0;
while (str[i] != '\0' && k < nbmot) {
j = 0;
while ((str[i] == ' ' || str[i] == '\n' || str[i] == '\t')
&& str[i] != '\0')
i++;
while (j < taillemot[k]) {
tab[k][j] = str[i];
j++;
i++;
}
//tab[k][j] = '\0';
i++;
k++;
}
tab[nbmot] = 0;
}
char **ft_split_whitespaces(char *str) {
int nbmot = ft_compte_mot(str);
int *taillemot;
int i = 0;
char **string;
if ((taillemot = (int*)malloc(sizeof(int) * nbmot)) == NULL)
return (NULL);
ft_compte_taille_mot(taillemot, str);
if ((string = (char **)malloc(sizeof(char *) * (nbmot + 1))) == NULL)
return (NULL);
while (i < nbmot) {
if ((string[i] = (char *)malloc(sizeof(char) * taillemot[i] + 1))
== NULL)
return (NULL);
i++;
}
ft_copy_word(taillemot, nbmot, string, str);
return (string);
}
void ft_putchar(char c) {
write(1, &c, 1);
}
void ft_putstr(char *str) {
int i = 0;
while (str[i] != '\0') {
ft_putchar(str[i]);
i++;
}
}
void ft_print_words_tables(char **tab) {
int i;
i = 0;
while (tab[i] != 0) {
ft_putstr(tab[i]);
ft_putchar('\n');
i++;
}
ft_putchar('\n');
}
EDIT : Here is the main EDIT 2 : I also tested with argv[1]
char **ft_split_whitespaces(char *str);
void ft_print_words_tables(char **tab);
void ft_putchar(char c)
{
write(1, &c, 1);
}
int main(int argc, char **argv)
{
ft_print_words_tables(ft_split_whitespaces(argv[1]));
return (0);
}
FYI i'm in school and we have a particular norm, we can't use for loop or a bunch of libc function.
I'm really stuck here and i really don't understand why it's work with " " but not with **argv
thx by advance for your help
I found the solution :
char *str;
str = argv[1];
str[strlen(str) + 1] = '\0';
ft_print_words_tables(ft_split_whitespaces(str));
return (0);
thanks for the help

splitting a string by white spaces

Before reading this question please note that my question pertains to a school assignment. For this assignment the only function we are allowed to use is malloc(). Everything else must be done without the use of other libraries.
I'm calling a function ft_split_whitespaces. This function takes a string as input and "splits" it into words. Words are separated spaces, tabs and line breaks.
#include <stdio.h>
char **ft_split_whitespaces(char *str);
int main(void)
{
char *str = "what is";
char **test = ft_split_whitespaces(str);
}
With respect to the example above the result at each index should be
test[0] = "what"
test[1] = "is"
test[2] = NULL
However, I am only able to print the results of test[0]. All other indices do not get printed to display. Heres an example of some code that I assume should print the results of my function.
int i = 0;
while(test[i] != NULL)
{
printf("%s", test[i]);
i++;
}
When this portion of code is ran, only test[0] is printed to the output. I've been sitting here trying to debug my code for hours. If anyone has some spare time and doesn't mind looking over my code, I'd appreciate it tremendously. I have a feeling it may be an issue with how I'm using malloc, but I still cant figure it out.
#include <stdlib.h>
#include <stdio.h>
int count_whitespaces(char *str)
{
int space_count;
int i;
space_count = 0;
i = 0;
while(str[i] != '\0')
{
if(str[i] == ' ' || str[i] == 9 || str[i] == '\n')
{
if(str[i+1] != ' ' && str[i+1] != 9 && str[i+1] != '\n')
space_count++;
}
i++;
}
return (space_count);
}
int check_whitespace(char c)
{
if (c == ' ' || c == 9 || c == '\n' || c == '\0')
{
return (1);
}
else
return(0);
}
int count_characters(char *str, int i)
{
int char_count;
char_count = 0;
while(str[i])
{
if(str[i+1] != ' ' && str[i+1] != 9 && str[i+1] != '\n')
char_count++;
else
break;
i++;
}
return (char_count);
}
char **ft_split_whitespaces(char *str)
{
int i;
int j;
int k;
char **word;
int space;
i = 0;
j = 0;
k = 0;
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
while(str[i] != '\0')
{
if (check_whitespace(str[i]) == 1)
i++;
else
{
if((word[j] = malloc(sizeof(char) * (count_characters(str, i) + 1))) == NULL)
return (NULL);
while (check_whitespace(str[i]) == 0)
{
word[j][k] = str[i];
i++;
k++;
}
j++;
}
}
j++;
word[j] = NULL;
j = 0
return word;
}
You forgot to reset k. The outer while loop in ft_split_whitespaces should look like that
while (str[i] != '\0') {
if (check_whitespace(str[i]) == 1){
i++;
}
else {
if ((word[j] =
malloc(sizeof(char) * (count_characters(str, i) + 1))) == NULL){
return (NULL);
}
while (check_whitespace(str[i]) == 0) {
word[j][k] = str[i];
i++;
k++;
}
word[j][k] = '\0';
j++;
k = 0; // reset k
}
}
So I actually figured out what was going wrong with my code as I finished typing this question (thanks stack overflow!). I decided to post it anyways, because I thought it might be a good learning experience for coding newbies such as myself.
This is where my issue was occurring.
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
while(str[i] != '\0')
{
if (check_whitespace(str[i]) == 1)
i++;
else
{
if((word[j] = malloc(sizeof(char) * (count_characters(str, i) + 1))) == NULL)
return (NULL);
while (check_whitespace(str[i]) == 0)
{
word[j][k] = str[i];
i++;
k++;
}
j++;
}
}
I malloc'd my char **word outside of the while loop using the following code
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
And then within the actual while loop I malloc'd it again using
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
Using malloc multiple times on the same variable was causing all sorts of weird issues. So within the while loop I ended up using a new variable I declared as char *words. Here is that portion of the while loop with the correct code.
word = (char**)malloc(sizeof(char*)*(count_whitespaces(str) + 2));
while(str[i] != '\0')
{
if (check_whitespace(str[i]) == 1)
i++;
else
{
words = malloc(sizeof(char) * (count_characters(str, i) + 1));
k = 0;
while (check_whitespace(str[i]) == 0)
{
words[k] = str[i];
i++;
k++;
}
words[k] = '\0';
word[j] = words;
j++;
}
}

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

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;
}

Resources