How to Fix Errors in C String Compression Function?

  • Thread starter Thread starter xortan
  • Start date Start date
  • Tags Tags
    String
Click For Summary
The discussion centers on writing a function in C to compress multiple spaces, tabs, and newlines into a single space in a string. The initial attempt encounters errors related to pointer usage and function return types. The user is advised to correctly handle pointer arithmetic and ensure that the destination pointer is properly initialized. Key issues include the incorrect use of logical operators in the condition checks and the need for a proper loop structure to skip over consecutive whitespace characters. The user later updates the code to include checks for tabs and newlines but struggles with the logic for detecting and replacing whitespace. The importance of using the correct syntax for loops and maintaining proper pointer manipulation is emphasized, along with the suggestion to utilize code formatting for better readability. Ultimately, the user finds that their function is working after further testing and adjustments.
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 6 ·
Replies
6
Views
3K
Replies
89
Views
6K
  • · Replies 4 ·
Replies
4
Views
1K
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 17 ·
Replies
17
Views
2K