在 C 中编写程序来反转字符串中的单词时遇到问题 [英] Having trouble writing program to reverse words in string in C

查看:63
本文介绍了在 C 中编写程序来反转字符串中的单词时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个小程序来反转字符串中的单词(经典面试问题),但我遇到了一些障碍.在网站的另一部分,我遇到了一种使用按位运算反转字符串的非常酷的方法,因此我尝试使用一个仅使用单个 reverse() 的程序,该程序将反转整个字符串并然后是子串.

I'm trying to write a little program to reverse the words in a string (the classic interview question) but I'm hitting a bit of a snag. On another portion of the site, I came across a really cool way to reverse strings in place using bitwise operations, so I'm attempting to use a program that only utilizes a single reverse() that will be called in reverse the entire string and then the substrings.

不幸的是,我的程序只会反转第一个子字符串,我很难找出原因.任何帮助将不胜感激.

Unfortunately, my program will only reverse the first substring and I'm having trouble finding out why. Any help would be much appreciated.

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void reverse(char string[], int beg, int end) {
  int length = end-beg-1;
  for(int i=beg; i < length/2; i++) {
    string[i] = string[i] ^string[length-i];
    string[length-i] = string[i] ^ string[length - i];
    string[i] = string[i] ^string[length-i];
  }

 }

int main (int argv, char * arrv[]) {
   int beg, end;
   char string[] = "This is the string to reverse";
   int length = strlen(string);
   printf("%s\n", string);
   printf("%d\n", length);
 //reverse whole string
   reverse(string, 0, length);
   printf("%s\n", string);
   beg =0;
   end = 0;
  //reverse strings at index's beg and end
    for(int i = 0; i <= length-1; i++) {
      if (string[i] == ' ') {                                      
       reverse(string, beg, end);
       beg = i+1;  
     }
     end++;
     printf("%s\n", string);
   }
   printf("%s\n", string);
    return 1;
}

谢谢IE预期输出:反向字符串是这个我的输出:reverse ot gnirts eht si siht

Thanks I.E Expected Output: reverse to string the is this My Output: reverse ot gnirts eht si siht

推荐答案

加上@alexd 的建议,以下是更新后的程序,有一些修复:

Adding up @alexd 's suggestion, following is the updated program with few fixes:

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void reverse(char string[], int beg, int end) {
    int length = end - beg;
    for (int i = 0; i < length / 2; i++) {
        string[beg + i] = string[beg + i] ^ string[end - 1 - i]; 
        string[end - 1 - i] = string[beg + i] ^ string[end - 1 - i]; 
        string[beg + i] = string[beg + i] ^ string[end - 1 - i]; 
    }   
}

int main (int argv, char * arrv[]) {
   int beg, end;
   char string[] = "This is the string to reverse";
   int length = strlen(string);
   printf("%s\n", string);
   printf("%d\n", length);
 //reverse whole string
   reverse(string, 0, length);
   printf("%s\n", string);
   beg =0; 
   end = 0;
  //reverse strings at index's beg and end
    for(int i = 0; i <= length-1; i++) {
      if (string[i] == ' ') {    
       reverse(string, beg, end);
       beg = i+1;  
       printf("%s %d %d\n", string, beg, end);
     } else if(i==length-1) {
       end = length;
       reverse(string, beg, end);
     }   
     end++;
 }
   printf("%s\n", string);
    return 1;
}

这篇关于在 C 中编写程序来反转字符串中的单词时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆