A
AMT2K5
Hello. I have the following function titled cleanSpace that recieves a
string and cleans it up. My program passes a string to the function via
"cleanSpace(c)". The function works the first time through, but the
second time through I recieve a seg fault [core dump] on the second
last line, strcpy.
I am certain something is wrong with the pointer and the way I use it.
Could somebody identify what I have done wrong at that line?
int cleanSpace(char* ptr)
{
char temp[300];
strcpy(temp, ptr);
int i=0,
j=0,
k=0,
m=0;
/* Ultimately this loop will scan for new lines and tabs and replace
them
with spaces. */
for(i=0; temp; i++)
{
if(temp == '\n' || temp == '\t')
temp = ' ';
}
// For loop finds character starting point.
for(i=0; temp == ' '; i++)
{
temp[m] = temp[i+1];
}
// For loop moves all characters next to the first found character.
for(i++; temp; i++)
{
temp[++m] = temp;
}
temp[m+1] = '\0';
// For loop removes trailing spaces.
for(i = strlen(temp) - 1; temp == ' '; i--)
{
temp = '\0';
}
// For loop removes excess spaces.
for(i = 0; temp; i++)
{
if(temp == ' ' && temp[i+1] == ' ')
{
j = i;
while(temp[j] == ' ')
{
j++;
}
for(k = i + 1; temp[k]; k++, j++)
{
temp[k] = temp[j];
}
j=0;
}
}
strcpy(ptr,temp); // Copy temp to ptr
return strlen(temp); // Return the length
}
Appreciate any feedback
- Aaron T
string and cleans it up. My program passes a string to the function via
"cleanSpace(c)". The function works the first time through, but the
second time through I recieve a seg fault [core dump] on the second
last line, strcpy.
I am certain something is wrong with the pointer and the way I use it.
Could somebody identify what I have done wrong at that line?
int cleanSpace(char* ptr)
{
char temp[300];
strcpy(temp, ptr);
int i=0,
j=0,
k=0,
m=0;
/* Ultimately this loop will scan for new lines and tabs and replace
them
with spaces. */
for(i=0; temp; i++)
{
if(temp == '\n' || temp == '\t')
temp = ' ';
}
// For loop finds character starting point.
for(i=0; temp == ' '; i++)
{
temp[m] = temp[i+1];
}
// For loop moves all characters next to the first found character.
for(i++; temp; i++)
{
temp[++m] = temp;
}
temp[m+1] = '\0';
// For loop removes trailing spaces.
for(i = strlen(temp) - 1; temp == ' '; i--)
{
temp = '\0';
}
// For loop removes excess spaces.
for(i = 0; temp; i++)
{
if(temp == ' ' && temp[i+1] == ' ')
{
j = i;
while(temp[j] == ' ')
{
j++;
}
for(k = i + 1; temp[k]; k++, j++)
{
temp[k] = temp[j];
}
j=0;
}
}
strcpy(ptr,temp); // Copy temp to ptr
return strlen(temp); // Return the length
}
Appreciate any feedback
- Aaron T