#include<stdio.h>
#include<string.h>

int main()
{

	int i,x;
	char c[100];

	printf("Enter input :");
	fgets(c,100,stdin);

	for( i = 0 ; i <= (x = (strlen(c))) ; i++)
	{

		if( c[i] = '\t')
		{
			c[i] = (' ');
                        printf("   ");
		}
		printf("%c",c[i]);
	}
}

the aim of this program is that it will replace tabs entered in the input with 4 spaces.

need help with this program .

this programe is giving compile error:-

SEGMENTATION FAULT

this programe is giving compile error:-

SEGMENTATION FAULT

No it isn't. A Seg fault is NOT a compiler error, but runtime error. The compiler has finished doing its job by the time you run the program.

line 18: c = (' ');

Remove the { and }. You don't put them around assignment values. Just make it c[i] = ' '; Are you supposed to replace the tab with 4 spaces on the screen only leaving the character array untouched ? Or are you supposed to put the 4 spaces in the character array? If the latter, then the program you write does not do what its supposed to do. When a tab is found, all remaining characters have to be moved to the right 3 places to open up enough room to insert three more spaces. Be careful not to overflow the character array when doing that.

Adding one more

if( c[i] = '\t')

will be always true


Bad coding:-

for( i = 0 ; i <= (x = (strlen(c))) ; i++)

Here every iteration will call strlen(c)....which is bad. Store the length once before loop starts.

Edited 6 Years Ago by Luckychap: n/a

Comments
Agree.
This article has been dead for over six months. Start a new discussion instead.