How to Fix Errors in C String Compression Function?

  • Thread starter xortan
  • Start date
  • Tags
    String
In summary: Then you want to increment s past any leading white space characters. Hence, the do-while loop. In summary, you are trying to write a function that removes multiple spaces, tabs, and newlines from a given string. You have encountered some errors while using a void expression and making an integer from a pointer without a cast. You have also noticed that PF already compresses spaces when entered into a box. After playing around with the code, you have successfully removed spaces and have figured out the proper way to call the function from main. You are now trying to detect tabs and newlines, but have encountered some difficulties. You will need to walk through the entire string and shift the string backwards each time you find whitespace. Your do-while
  • #1
xortan
78
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
  • #2
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;
}
 
  • #3
you will need to walk through the entire string and shift the string backwards one each time you find whitespace
 
  • #4
I thought that's why I had --s there. It is able to compress spaces but not tabs
 
  • #5
Sorry actually I think its working, wasn't testing it properly I guess
 
  • #6
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:
  • #7
Also, use code tags and you will retain the indentation, extra spaces, etc.
 
  • #8
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:

Related to How to Fix Errors in C String Compression Function?

1. How do I compress a string in C?

To compress a string in C, you can use a library or algorithm such as zlib or gzip. These libraries provide functions and methods for compressing and decompressing data, including strings. You can also implement your own compression algorithm, but using a library is often more efficient and reliable.

2. Why should I compress a string in C?

Compressing a string in C can help reduce its size, making it easier to store and transfer. This is particularly useful when dealing with large amounts of data, as it can save memory and improve performance. Compression can also improve security by making the data more difficult to read or modify.

3. How do I decompress a compressed string in C?

To decompress a compressed string in C, you can use the inverse function or method of the compression library or algorithm you used. For example, if you compressed the string with zlib, you can use inflate() to decompress it. You will need to provide the compressed string and a buffer to store the decompressed data in.

4. Can I compress any type of string in C?

Yes, you can compress any type of string in C, including alphanumeric strings, special characters, and even binary data. However, the effectiveness of the compression may vary depending on the type of data. For example, compressing a string of random characters may not result in much reduction in size, while compressing a string of repeated characters may greatly reduce its size.

5. Are there any limitations to compressing strings in C?

Yes, there are some limitations to compressing strings in C. One limitation is that the compressed string cannot be larger than the original string. Additionally, some compression algorithms may not be as effective for certain types of data, so you may need to experiment with different algorithms to find the most efficient one for your specific string.

Similar threads

  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
857
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
34
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
10
Views
2K
Back
Top