我的程序有问题 [英] Something wrong in my program
问题描述
大家好,
我不太喜欢C,而且我很难用琴弦。我正在尝试
要做的是一个简单的功能,可以修剪空间和
给定字符串开头的制表符。例如,我想要这个: Hello World成为这个:
Hello World。乍一看,我的功能似乎工作,但返回一些
奇怪的价值......
这是我的代码(请原谅这个烂摊子) :
#include< stdio.h>
#include< string.h>
int main(void){
char * trimbegin(char * text);
char * str =" Hello World!" ;;
char * result = trimbegin(str);
printf("函数返回的内容:\"%s \" \ n",result);
返回0;
}
char * trimbegin(char * text){
int i = 0,j = 0,ok = 0;
int size = strlen(text);
char buffer [size + 1 ];
char * ptr;
printf("原文是:\"%s \" \ n",text );
printf(那是'%d个字符长... \ n,大小);
printf(" Now,我们的文本缓冲区可以包含%d个字符\ n",size +
1);
for(i = 0; i< = size; i ++){
if(ok == 1){
buffer [j] = text [i];
j ++;
}
else if(isspace(text [i])== 0&& ok == 0){
buffer [j] = text [i];
j ++;
ok = 1;
}
}
printf("结果应该是什么: \"%s \" \ n",buffer);
ptr = buffer;
return ptr;
}
这里有输出:
[dom @ localhost C] $ ./a.out
原文为: Hello World!"
那个'是14个字符长...
现在,我们的文本缓冲区可以包含15个字符
什么结果应该是:Hello World!
函数返回的内容:Hello World! @ 8 @ $÷?????? PS @ y @è???"
为什么它会返回Hello World! @ 8 @ $÷?????? PS @ý@è???"而不是Hello
World!?
感谢您的帮助!
-Dom
./ a.out
原文为:" Hello World!"
那个'是14个字符长...
现在,我们的文本缓冲区可以包含15个字符
什么结果应该是:Hello World!
函数返回的内容:Hello World! @ 8 @
÷?????? PS @ y @è???"
为什么它会返回Hello World! @ 8 @
÷?????? PS @ý@è???"而不是Hello
World!?
感谢您的帮助!
-Dom
Hello guys,
I''m kinda new to C, and I''m having a hard time with strings. What I''m trying
to do is a simple function that trims spaces & tabs at the beginning of a
given string. For example, I want this: " Hello World" to become this:
"Hello World". At first glance, my function seems to work, but returns some
strange values...
Here''s my code (please pardon the mess):
#include <stdio.h>
#include <string.h>
int main(void){
char *trimbegin(char *text);
char *str = " Hello World!";
char *result = trimbegin(str);
printf("What the function returns: \"%s\"\n", result);
return 0;
}
char *trimbegin(char *text){
int i = 0, j = 0, ok = 0;
int size = strlen(text);
char buffer[size + 1];
char *ptr;
printf("Original text is: \"%s\"\n", text);
printf("That''s %d characters long...\n", size);
printf("Now, our text buffer can contain %d characters\n", size +
1);
for (i = 0; i <= size; i++){
if (ok == 1){
buffer[j] = text[i];
j++;
}
else if (isspace(text[i]) == 0 && ok == 0){
buffer[j] = text[i];
j++;
ok = 1;
}
}
printf("What the result is supposed to be: \"%s\"\n", buffer);
ptr = buffer;
return ptr;
}
And here''s the output:
[dom@localhost C]$ ./a.out
Original text is: " Hello World!"
That''s 14 characters long...
Now, our text buffer can contain 15 characters
What the result is supposed to be: "Hello World!"
What the function returns: "Hello World! @8@$÷??????PS@y@è???"
Why does it return "Hello World! @8@$÷??????PS@y@è???" and not "Hello
World!"?
Thanks for all your help!
-Dom
./a.out
Original text is: " Hello World!"
That''s 14 characters long...
Now, our text buffer can contain 15 characters
What the result is supposed to be: "Hello World!"
What the function returns: "Hello World! @8@
÷??????PS@y@è???"
Why does it return "Hello World! @8@
÷??????PS@y@è???" and not "Hello
World!"?
Thanks for all your help!
-Dom
这篇关于我的程序有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!