`我试图用一个单词的位置来修改句子中的每个单词。 这句话虽然(c!='')每次都有段错误。有人可以让我知道我哪里出错了吗?这是我的代码。
#include<stdio.h>
void swap(char *i, char *j)
{
char t;
t = *i;
*i = *j;
*j = t;
}
void reverse(char *s, char *e)
{
char *i, *j;
i = s;
j = e;
while(i <= j)
{
swap(i, j);
i++;
j--;
}
}
int main()
{
int check = 0;
char *a= (char*) malloc(100*sizeof(char));
char *c, *b, *t;
char *s = ' ';
printf("enter your sentence\n");
fgets (a, 100, stdin);
if ((strlen(a) > 0) && (a[strlen(a)-1] == '\n'))
a[strlen(a)-1] = '\0';
printf("\nyour stat: %s and size is %d\n", a, strlen(a));
b = a;
c = a;
while(*b != ' ')
b++;
b--;
while(!check)
{
reverse(c, b);
t = c;
c = b;
b = t;
while(*c != ' ')// segmentation fault :|
c++;
while(*c == ' ')
c++;
b++;
while(*b == ' ')
b++;
while((*b != ' ') && (*b != '\0'))
{
if(*b = '\0')
{
check = 1;
b--;
reverse(c, b);
break;
}
b++;
}
b--;
}
printf("\n reversed stat is : %s\n",a);
return 0;
}
答案 0 :(得分:1)
您可以在几个地方改进代码,使其更加强大。真正的问题是你正在使用
if(*b = '\0') // This assigns the null character to *b
而不是
if(*b == '\0') // This compare whether *b is the null character
<强>更新强>
您可以采取哪些措施来改进代码:
您可以使用数组,而不必使用malloc
。
而不是
char *a= (char*) malloc(100*sizeof(char));
使用
char a[100];
编写一对辅助函数来帮助跳过空白区域并跳过非白色空格。
char* skipWhiteSpace(char* in)
{
while (isspace(*in) && *in != '\0') ++in;
return in;
}
char* getEndOfNonWhiteSpace(char* in)
{
while (!isspace(*in) && *in != '\0') ++in;
return in-1;
}
然后,main
的核心可以简化。 main
可以是:
int main()
{
char a[100];
char *c, *b;
printf("enter your sentence\n");
fgets (a, 100, stdin);
if ((strlen(a) > 0) && (a[strlen(a)-1] == '\n'))
a[strlen(a)-1] = '\0';
printf("\nyour stat: %s and size is %zu\n", a, strlen(a));
c = a;
while(1)
{
c = skipWhiteSpace(c);
b = getEndOfNonWhiteSpace(c);
reverse(c, b);
c = b+1;
if (*c == '\0' )
{
break;
}
}
printf("\n reversed stat is : %s\n",a);
return 0;
}
答案 1 :(得分:1)
失败的行将很乐意继续超过字符串的结尾。
您还需要检查字符串的结尾:
while(*c && *c != ' ')
答案 2 :(得分:0)
使用if(*b = '\0')
if(*b == '\0')
这里:
while((*b != ' ') && (*b != '\0'))
{
if(*b = '\0') // here is problem change it with if(*b == '\0')
{
check = 1;
b--;
reverse(c, b);
break;
}
b++;
}
答案 3 :(得分:0)
if(*b = '\0')
您的这种情况是将'\0'
分配给*b
,而不是*b
签入等于'\0
。将其更改为
if(*b == '\0')
答案 4 :(得分:0)
一些建议
此版本还解决了不同平台上的换行问题。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main (int argc, char *argv[])
{
char *sentence;
char *gap;
char *space = " ";
char *reversed;
size_t size;
int len = 0;
if (-1 == getline (&sentence, &size, stdin)) {
return -1;
}
len = strlen (sentence);
/* Is a line-feed the last character ? */
gap = strrchr (sentence, '\n');
if (NULL != gap) {
sentence[--len] = '\0';
}
/* Is a carriage return the last character ? */
gap = strrchr (sentence, '\r');
if (NULL != gap) {
sentence[--len] = '\0';
}
reversed = malloc (len);
if (NULL == reversed) {
return -2;
}
/* is there a right-most space character ? */
while (NULL != (gap = strrchr (sentence, ' '))) {
/* turn it into a string end */
*(gap++) = '\0';
/* append the "word" after the gap to the reversed buffer */
strcat (reversed, gap);
strcat (reversed, " ");
}
/* the last "word" must be appended, too */
strcat (reversed, sentence);
printf ("Reversed: %s\n", reversed);
free (reversed);
return 0;
}