I can't figure out why my program is not working.

Joined
May 23, 2024
Messages
1
Reaction score
0
Hello everyone, this is my first post here.

This program is supposed to, if more than one space is found, only print one and then continue printing the line and do the same for any other spaces that are more than one.

C:
#include <stdio.h>

int getandshort(char store[], char hold[]);
int nullit(char arr[]);
int main()
{
int i;
char storeline[100];
char holdspace[100];
nullit(storeline);
nullit(holdspace);

getandshort(storeline, holdspace);

for(i = 0; i < 100; ++i)
        printf("%c", storeline[i]);
return 0;
}


int getandshort(char store[], char hold[])
{
int i;
int c;
for(i = 0; (c = getchar()) != '\n' && c != EOF; ++i)
{
        if(c != ' ' || c == ' ' && store[i - 1] != ' ')
                store[i] = c;
}
store[i] = '\0';
}

int nullit(char arr[])
{
int i;
  for(i = 0; i < 100; ++i)
  {
    arr[i] = '\0';
  }
}

When I run it, it shortens the trail of spaces but I think it only removes one.
What's up with this?
 
Joined
Sep 21, 2022
Messages
126
Reaction score
16
There's more than one problem, but the main one is inside getandshort(), you're incrementing i every time around the loop.

The i++ should be inside the IF statement, after store[i] = c;

Welcome to the forum.
 
Joined
Jul 4, 2023
Messages
399
Reaction score
44
Your functions:
  • getandshort
  • nullit
they return nothing, use void
C:
void getandshort(char store[], char hold[]);
void nullit(char arr[]);

//...

void getandshort(char store[], char hold[])
{
    // ...
}
void nullit(char arr[])
{
    // ...
}

this part
C:
for(i = 0; (c = getchar()) != '\n' && c != EOF; ++i)
write down simply like that
C:
for(i = 0; store[i] != '\0'; ++i)

check out this example
C:
void getandshort(char store[])
{
    int i, j = 0;
    int wasSpace = 0;
    for (i = 0; store[i] != '\0'; ++i)
    {
        if (store[i] != ' ' || (store[i] == ' ' && !wasSpace))
        {
            store[j++] = store[i];
            wasSpace = (store[i] == ' ');
        }
    }
    store[j] = '\0';
}
  1. Initialize indices i and j to 0.
  2. Initialize wasSpace to 0 (false).
  3. Loop through each character in store until the null terminator is reached - '\0'.
  4. If the character is not a space, or it is a space but the previous character was not a space (!wasSpace):
    • Copy the character to the position j in store.
    • Increment j.
    • Set wasSpace to 1 if the character is a space, otherwise set it to 0.
  5. After the loop, add a null terminator at position j in store.

full example:
C:
#include <stdio.h>

void getandshort(char store[]); // not used: char hold[]
void nullit(char arr[]);
void print(char arr[]);

int main()
{
    char storeline[100];
    // not used: char holdspace[100];
 
    nullit(storeline);
    // not used: nullit(holdspace);

    char loremIpsum[] = "  Lorem ipsum  dolor sit amet,  consectetur adipiscing     elit, sed  do eiusmod   tempor incididunt.";

    // Copy loremIpsum to storeline
    for (int i = 0; loremIpsum[i] != '\0' && i < 100; i++)
        storeline[i] = loremIpsum[i];

    getandshort(storeline); // not used: holdspace

    printf("Before:\n");
    print(loremIpsum);
 
    printf("\nAfter:\n");
    print(storeline);
  
    return 0;
}

void getandshort(char store[]) // not used: char hold[]
{
    int i, j = 0;
    int wasSpace = 0;
    for (i = 0; store[i] != '\0'; ++i)
    {
        if (store[i] != ' ' || (store[i] == ' ' && !wasSpace))
        {
            store[j++] = store[i];
            wasSpace = (store[i] == ' ');
        }
    }
    store[j] = '\0';
}

void nullit(char arr[])
{
    for (int i = 0; i < 100; ++i)
         arr[i] = '\0';
}

void print(char arr[])
{
    for (int i = 0; arr[i] != '\0'; i++)
         printf("%c", arr[i]);
    printf("\n");
}
 
Last edited:
Joined
Sep 21, 2022
Messages
126
Reaction score
16
Code:
store[i] != ' ' || (store[i] == ' ' && !wasSpace)

store[i] == ' ' is redundant

A     : store[i] != ' '
not(A): store[i] == ' '
B     : !wasSpace

A   B   A or (not(A) and B)
F   F   F
F   T   T
T   F   T
T   T   T

That is equivalent to A or B

store[i] != ' ' || !wasSpace
 
Joined
Jul 4, 2023
Messages
399
Reaction score
44
Code:
A : store[i] != ' '
not(A): store[i] == ' '
 B : !wasSpace

A B A or (not(A) and B)
 F F F
 F T T
 T F T
 T T T

 That is equivalent to A or B

 store[i] != ' ' || !wasSpace
Thank you, that's true, I didn't think about it that way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,820
Messages
2,569,722
Members
45,510
Latest member
ThaliaMaxi

Latest Threads

Top