I am trying to write a program that reads a string and shortens it if the letters are in a row. For instance abcdghigjhkhdf becomes a-dg-igkhkhdf. I want that it erases the ;etters it erases but it is not working. HEre is my code:

#include <stdio.h>

#define MAXSTRING 100 
void main()
{
	char str[MAXSTRING]={0};

	printf("Please input a string of letters without a space or numbers.");
	scanf_s("%s", &str);

	convertString(str);

}

	void convertString(char string[])
	{
		int i=0;
		char startOfNewConinuation=0;
		
		while (string[i]!= 0)
		{
			if (string[i]!= '-')
				startOfNewConinuation=string[i];

			if ((string[i+1]=string[i]+1) && (startOfNewConinuation=string[i]))
				string[i+1]='-';
			else
				string[i]='';
			i++;
		}
		printf("\nThe new string is %s",string);
	}

Recommended Answers

All 3 Replies

I dont understand your logic. But I think you are supposed to use relational operator == instead of = in if condition.
if ((string[i+1]==string+1) && (startOfNewConinuation==string))

I am trying to write a program that reads a string and shortens it if the letters are in a row. For instance abcdghigjhkhdf becomes a-dg-igkhkhdf. I want that it erases the ;etters it erases but it is not working. HEre is my code:

#include <stdio.h>

#define MAXSTRING 100 
void main()
{
	char str[MAXSTRING]={0};

	printf("Please input a string of letters without a space or numbers.");
	scanf_s("%s", &str);

	convertString(str);

}

	void convertString(char string[])
	{
		int i=0;
		char startOfNewConinuation=0;
		
		while (string[i]!= 0)
		{
			if (string[i]!= '-')
				startOfNewConinuation=string[i];

			if ((string[i+1]=string[i]+1) && (startOfNewConinuation=string[i]))
				string[i+1]='-';
			else
				string[i]='';
			i++;
		}
		printf("\nThe new string is %s",string);
	}

> line #4:Start using int main() instead of void main().
> line #25: when u reach the end, say i=3 and length of string is 4, u are accessing the (i+1)th element that is arr[4] which does not exist because your string ends at index 3. Try to correct it.
> your logic won't give u waht u are trying. Check it agin.

> if the input to your logic is "a-dgh" the output according to your problem definition should be "a-dgh" only but if u try to get the original string back u would end up with getting: "abcdgh". Note that.

> here is what u can try to achieve your task.

void compress(char* input)
{
        if(input==NULL)
                return;
        int ilen = strlen(input);
        if(ilen<3)
                return;
        int i=1, j=0;
        char* result = (char*)malloc(ilen+1);

        int seq_start_index = 0;
        while(input[i]!=0)
        {
                if(input[i]-1!=input[i-1])
                {
                        result[j++] = input[seq_start_index];/*sequence starting char*/
                        if(i-seq_start_index>2)/*if the sequence length is more than 2 add '-' and the end char*/
                        {
                                result[j++] = '-';
                                result[j++] = input[i-1];
                        }
                        else if(i-seq_start_index==2)/*if the length is 2 then dont add the '-'char*/
                                result[j++] = input[i-1];
                        seq_start_index = i;/*start a new seq*/
                }
                i++;
        }
        if(seq_start_index<i-1)/*if the seq was not processed, e.g. if input was "abdefgh", the above loop will retrieve only "ab", d-h is not yet added*/
        {
                result[j++] = input[seq_start_index];
                if(i-seq_start_index>2)
                {
                        result[j++] = '-';
                        result[j++] = input[i-1];
                }
                else if(i-seq_start_index==2)
                        result[j++] = input[i-1];
        }
        else
                result[j++] = input[seq_start_index];
        result[j] = '\0';/*string termination*/
        strcpy(input, result);
        return;
}

Go thru it and see if u can understand what I have done, else u can ask.

The following code also may help you

#include <stdio.h>
int main()
{
int i,k,cnt,j;
char str[30], str1[30];
printf("Enter a string");
gets(str);  // str is the input string , str1 is the resultant string
for(i=0,k=0;str[i] != '\0' ; i++)
{
	for(str1[k]=str[i],j=i+1,cnt=1; str[j] != '\0' ; j++,cnt++)
	{
		if( (str[i] + cnt) != str[j]) break;
	}
	if(cnt > 1)
	{
		if(cnt != 2) str1[++k] = '-';
		str1[++k] = str[j-1];
		i=i+cnt-1;
	}
	k++;
}
str1[k] = '\0';
puts(str1);
return;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.