使用指针(必须使用unsigned *char)在C中反转一个字符串。

huangapple go评论88阅读模式
英文:

reverse a string using pointers (have to use unsigned *char) in C

问题

以下是您提供的代码的翻译部分:

  1. 我目前正在为大学的作业而工作。
  2. 任务是在一个单独的函数中反转一个字符串。函数名如下所示:
  3. > `void string_rev(unsigned char *str);`
  4. 我的解决方案如下:
  5. ```c
  6. void string_rev(unsigned char *str){
  7. int length = 0;
  8. int counter = 0;
  9. unsigned char *endptr = str;
  10. for (int i = 0; str[i] != '
    我目前正在为大学的作业而工作。
  11. 任务是在一个单独的函数中反转一个字符串。函数名如下所示:
  12. > `void string_rev(unsigned char *str);`
  13. 我的解决方案如下:
  14. ```c
  15. void string_rev(unsigned char *str){
  16. 	int length = 0;
  17. 	int counter = 0;
  18. 	unsigned char *endptr = str;   
  19. 	for (int i = 0; str[i] != '\0'; i++)  //获取字符串的长度
  20. 		length++;
  21. 	for (int i = 0; i < length; i++)     //将endptr放到字符串的末尾
  22. 		endptr++;
  23. 	while (counter < length/2){      //交换从开头到末尾的值,直到字符串的一半
  24. 		char temp = *str;
  25. 		*str = *endptr;
  26. 		*endptr = temp;
  27. 		str++;
  28. 		endptr--;
  29. 		counter++;
  30. 	}
  31. 	for (int i = 0; i<length; i++){
  32. 		printf("%c", *str);
  33. 		str++;
  34. 	}
  35. }
  36. int main (void){
  37. 	char *array = "Hello";
  38. 	unsigned char *ptr = (unsigned char*)array;
  39. 	string_rev(ptr);
  40. 	return 0;
  41. }
  42. '; i++) //获取字符串的长度
  43. length++;
  44. for (int i = 0; i < length; i++) //将endptr放到字符串的末尾
  45. endptr++;
  46. while (counter < length/2){ //交换从开头到末尾的值,直到字符串的一半
  47. char temp = *str;
  48. *str = *endptr;
  49. *endptr = temp;
  50. str++;
  51. endptr--;
  52. counter++;
  53. }
  54. for (int i = 0; i<length; i++){
  55. printf("%c", *str);
  56. str++;
  57. }
  58. }
  59. int main (void){
  60. char *array = "Hello";
  61. unsigned char *ptr = (unsigned char*)array;
  62. string_rev(ptr);
  63. return 0;
  64. }

我收到的错误是“总线错误10!但我找不到错误。

这可能与unsigned char*有关,但我无法让它工作。有人可以帮忙吗?

FYI -> 我们必须使用unsigned char*!当然要使用指针!

谢谢 使用指针(必须使用unsigned *char)在C中反转一个字符串。

  1. <details>
  2. <summary>英文:</summary>
  3. I am currently working on an assignment for Uni.
  4. The task is to reverse a string in a separate function. The function-name is given like that:
  5. &gt; `void string_rev(unsigned char *str);`
  6. My Solution looks like this:

void string_rev(unsigned char *str){

  1. int length = 0;
  2. int counter = 0;
  3. unsigned char *endptr = str;
  4. for (int i = 0; str[i] != &#39;
    int length = 0;
  5. int counter = 0;
  6. unsigned char *endptr = str;   
  7. for (int i = 0; str[i] != &#39;\0&#39;; i++)  //getting length of string
  8. length++;
  9. for (int i = 0; i &lt; length; i++)     //putting endptr to end of string
  10. endptr++;
  11. while (counter &lt; length/2){      //switch values from start to end until half of string
  12. char temp = *str;
  13. *str = *endptr;
  14. *endptr = temp;
  15. str++;
  16. endptr--;
  17. counter++;
  18. }
  19. for (int i = 0; i&lt;length; i++){
  20. printf(&quot;%c&quot;, *str);
  21. str++;
  22. }
  23. &#39;; i++) //getting length of string
  24. length++;
  25. for (int i = 0; i &lt; length; i++) //putting endptr to end of string
  26. endptr++;
  27. while (counter &lt; length/2){ //switch values from start to end until half of string
  28. char temp = *str;
  29. *str = *endptr;
  30. *endptr = temp;
  31. str++;
  32. endptr--;
  33. counter++;
  34. }
  35. for (int i = 0; i&lt;length; i++){
  36. printf(&quot;%c&quot;, *str);
  37. str++;
  38. }

}

int main (void){

  1. char *array = &quot;Hello&quot;;
  2. unsigned char *ptr = (unsigned char*)array;
  3. string_rev(ptr);
  4. return 0;

}

  1. The Error I get is Bus Error 10! But I can&#39;t find the mistake.
  2. It may has to do with the unsigned char* but I don&#39;t get it to work. Can someone please help?
  3. FYI -&gt; We have to use unsigned char*! And of course pointers!
  4. Thank you :)
  5. </details>
  6. # 答案1
  7. **得分**: 5
  8. 请用以下方式替换:
  9. ```cpp
  10. char array[] = "Hello";
英文:

You are trying to modify a string constant. Replace

  1. char *array = &quot;Hello&quot;; // Not an array.

with

  1. char array[] = &quot;Hello&quot;;

huangapple
  • 本文由 发表于 2020年1月4日 00:50:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定