P
Paul
hi, there,
In the following codes, If I change the varible "string "in main()
to char* string=" this is the test string "; the program will
be crashed. can someone tell me why and how to modify the code so that
I can still use char * string? thanks.
================================
#include <stdio.h>
char *Trim(char *string)
{
char *from, *to;
int spaces = 0;
/* skip leading spaces */
for (from = to = string; *from ==' '; ++from);
for (;*from != '\0'; ++from)
{
if (*from != ' ')
{
if (spaces == 1) *to++ = ' ';
spaces = 0;
*to++ = *from;
}
else spaces = 1;
}
*to = '\0';
return string;
}
int main(void)
{
char string[] = " This is a test of the
string trim ";
printf("Before: \"%s\"\n",string);
string=Trim(string);
printf("After: \"%s\"\n",string);
return 0;
}
============================================================
In the following codes, If I change the varible "string "in main()
to char* string=" this is the test string "; the program will
be crashed. can someone tell me why and how to modify the code so that
I can still use char * string? thanks.
================================
#include <stdio.h>
char *Trim(char *string)
{
char *from, *to;
int spaces = 0;
/* skip leading spaces */
for (from = to = string; *from ==' '; ++from);
for (;*from != '\0'; ++from)
{
if (*from != ' ')
{
if (spaces == 1) *to++ = ' ';
spaces = 0;
*to++ = *from;
}
else spaces = 1;
}
*to = '\0';
return string;
}
int main(void)
{
char string[] = " This is a test of the
string trim ";
printf("Before: \"%s\"\n",string);
string=Trim(string);
printf("After: \"%s\"\n",string);
return 0;
}
============================================================