How to Fix Errors in C String Compression Function?

  • Thread starter Thread starter xortan
  • Start date Start date
  • Tags Tags
    String
Click For Summary

Discussion Overview

The discussion focuses on a C programming function intended to compress strings by replacing multiple spaces, tabs, and newlines with a single character. Participants explore various coding approaches, syntax issues, and logical errors in the implementation.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses confusion about errors related to void expressions and pointer casting in their initial implementation of the compress function.
  • Another participant shares an updated version of the code, indicating attempts to detect tabs and newlines, but struggles with logical conditions in the if statement.
  • One participant suggests that the function needs to shift the string backwards when whitespace is found.
  • A later reply points out a syntax error in the do-while loop and clarifies that decrementing the pointer does not replace characters in the string.
  • Another participant advises against using the decrement operation and emphasizes the need to increment past non-leading whitespace characters while copying the string.
  • One participant notes that the function seems to work after further testing, indicating potential issues with initial testing methods.
  • Another participant recommends using code tags to maintain formatting in the shared code snippets.

Areas of Agreement / Disagreement

Participants express varying levels of understanding and approaches to the problem, with some agreeing on the need for specific corrections while others propose different methods. The discussion remains unresolved regarding the optimal implementation of the function.

Contextual Notes

Limitations include potential misunderstandings of pointer manipulation, syntax errors, and the handling of whitespace characters. There are unresolved issues regarding the correct logic for detecting and replacing whitespace.

xortan
Messages
76
Reaction score
1
I am trying to write a function that will detect multiple spaces, tabs, and newlines and replace it with a single string. Here is what I got so far, I keep getting errors about using a void expression and making an integer from a pointer without a cast. Please help I am really new to programming.

void compress(char* s);


int main()
{
char a[] = "Hello World";

printf("%s", compress(a));

getchar();
}

void compress(char* s)
{

char *srcIx = s;
char *destIx;

while(*srcIx)
{
if(*srcIx != ' ' || *srcIx != '\t' || *srcIx != '\n')
*(destIx++) = *srcIx;

srcIx++
}

*s = destIx;

}

Am I even going about this problem the right way? Also when I enter a bunch of spaces into the box PF seems to compress it already :P Imagine there are a bunch of spaces between hello and world
 
Technology news on Phys.org
Sorry for the double post but was playing around with it and got it to be able to remove spaces and got figured out proper way to call function from main. Anyways here is some updated code, I am trying to detect tabs and newlines now. I tried adding some logical ANDS and ORS to the if statemant and while loop but doesn't detect it.

void compress(char* s)
{

char *srxIx = s;
for(; *s; ++s)
{
*srcIx++ = *s;
if(isspace(*s) || *s == '\t' || *s == '\n')
{
do
++s;
while(isspace(*s) || *s == '\t' || *s == '\n')
--s;
}
}
*srxIx = 0;
}
 
you will need to walk through the entire string and shift the string backwards one each time you find whitespace
 
I thought that's why I had --s there. It is able to compress spaces but not tabs
 
Sorry actually I think its working, wasn't testing it properly I guess
 
Your do while loops syntax is off

do
{
//CODE
}while(condition);

--s; is not replacing the string it is only decrementing the place it was previously, you need to physically replace after you decrement the string.
 
Last edited:
Also, use code tags and you will retain the indentation, extra spaces, etc.
 
You don't need the --s. You want to increment s past any non-leading white space characters, with the rest of the code copying from s to srcIx (not sure why you chose this name for a destination pointer).
 
Last edited:

Similar threads

Replies
5
Views
2K
Replies
89
Views
7K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
6
Views
2K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 23 ·
Replies
23
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K