空终止字符数组(C-字符串)不打印而不for循环

问题描述:

**我不能与此程序中使用的载体,或任何功能从标准库。因此,我为什么自己写这一切。空终止字符数组(C-字符串)不打印而不for循环

好了,这个计划就要写完了。我的所有用户定义的函数都工作正常,除了reverseCString函数。当我运行程序并输入一串“hello”时,我可以选择菜单选项“rev”来反转字符串。然后,我的主函数调用reverseCString函数,并使用我的main中的for循环打印出反转的c字符串。该方案在这一点上工作得很好,直到do-while循环继续..右心室功能被称为

后虽然,程序循环继续允许用户修改自己的字符串,因为它应该。但是,我的C字符串在这一点上消失了。如果我使用其他命令/功能,但仍可以对其执行操作,但c字符串不会打印到cout。

我不understnad是什么原因造成这一点,但我已经分离的问题倒在reverseCString功能。

这是到目前为止我的代码:

#include<iostream> 
#include<stdlib.h> 
#include<cstring> 
#include<string> 
#include<math.h> 

using namespace std; 


void shiftLeft(char szString[], size_t shiftBy) 
{ 
    const char *p = szString;// 
    while (*p) ++p;//advance p to point to the the null terminator, found via personal research at http://stackoverflow.com/questions/12201815/what-difference-between-whilepp-while-p-and-whilep 
    size_t len = p - szString;//len is set to the size of our c-string 

    if (len > 1 && (shiftBy %= len)) 
    { 
     char *ends[] = { szString+shiftBy, szString+len, szString+(len - shiftBy) };//create a temporary array for storage 
     for (size_t i = 0; i < 3; ++i)//use a for loop to flip the first three character 
     { 
      char *start = szString, *end = ends[i]; 
      while (start < --end)//now flip the rest of the characters 
      { 
       char ch = *start; 
       *start++ = *end; 
       *end = ch; 
      } 
     } 
    } 
} 


void shiftRight (char szString[], int size, int shiftBy) 
{ 
    if(shiftBy > size){ 
     shiftBy = shiftBy - size; 
    } 
    if(size == 1){ 
     //do nothing, exit function with no change made to myarray 
    } 
    else{ 
     char temp; 
     //for loop to print the array with indexes moved up (to the right) --> by 2 
     for (int i=0; i < size; i++) 
     {//EXAMPLE shift by 3 for a c-string of 5 
      temp = szString[size-shiftBy];//temp = myarray[2] 
      szString[size-shiftBy] = szString[i];//myarray[2] = myarray[i] 
      szString[i] = temp;//myarray[i] = temp(value previously at index 2) 
     } 

    } 
} 


void reverseCString(char szString[], const size_t& size){ 
    char temp; 
    int i = 0; 
    int j = size-1; 

    //we can use a simple tep variable to flip everything 
    while(i < j) 
    { 
     temp = szString[i]; 
     szString[i] = szString[j]; 
     szString[j] = temp; 
     i++; 
     j--; 
    } 
} 

    int main(){ 

     string repeat = ""; 

     string userInputString; 
      cout << "Please eneter your string: " << endl; 
      //cin >> ws; 
      getline(cin,userInputString); 


      char * szString = new char [userInputString.length()+1]; 
      strcpy (szString, userInputString.c_str()); 



     do { 


      cout << "Your c-string is: " << szString << endl; 

      string commandString = ""; 
      cout << "Please enter a command: "; 
      getline(cin,commandString); 

      if (commandString[0] == 'L'){ 
       cout << "You have chosen to shift your string left by " << commandString[1] << " characters." << endl; 
       const char * shiftLeftPtr = &commandString[1]; 
       //convert this value to an int for our function 
       int shiftLeftBy = atoi(shiftLeftPtr); 
       //run our shifleft function 
       shiftLeft(szString,shiftLeftBy); 
       //print the results 
       cout << "Your c-string, shifted left by " << commandString[1] << endl; 
       cout << szString; 

      } 

      if (commandString[0] == 'R'){ 
       cout << "You have chosen to shift your string right by " << commandString[1] << " characters." << endl; 
       const char * shiftRightPtr = &commandString[1]; 
       //convert this value to an int for our function 
       int shiftRightBy = atoi(shiftRightPtr); 
       //run our shiftright function 
       shiftRight(szString,userInputString.length(),shiftRightBy); 
       //print the results 
       cout << "Your c-string, shifted right by " << commandString[1] << endl; 
       cout << szString; 

      } 

      if (commandString.compare("rev") == 0){ 
       cout << "You have chosen to reverse your string. " << endl; 
       //run our reverseArray function 
       reverseCString(szString,userInputString.length()+1); 

       cout << "Your c-string, reversed: "; 
       for(int i = 0; i < userInputString.length()+1; i++){ 
///////////////////////////right her seems to be my issue 
       cout << szString[i]; 
       } 

      } 

      if (!(commandString[0] == 'R' || commandString[0] == 'L' || commandString.compare("rev") == 0)){ 
       cout << "You have entered an invalid selection." << endl; 
      } 

      cout << "\nEnter 'quit' to close the program, anything else to continue: "; 
      getline(cin,repeat); 

     }while(repeat.compare("quit") != 0); 

     return 0; 
    } 
+0

抱歉,没有标准的库函数可用于扭转的字符串。 – FluffyKittens 2014-11-06 04:27:07

+0

您是否通过示例字符串(无论是在纸上还是在调试器)执行代码的逻辑,看看它会做什么? – 2014-11-06 04:34:39

+0

你打赌我做了,我已经做了好几次。该函数正在工作,我的C字符串被颠倒。这个问题似乎在我的主要地方,当C字符串神奇地消失。 – FluffyKittens 2014-11-06 04:36:20

你长的逻辑被打破了。假设字符串包含“bar \ 0”。您这样做:

  reverseCString(szString,userInputString.length()+1); 

现在,长度为3,并且您将4传递给reverseCString。那么出现这种情况:

void reverseCString(char szString[], const size_t& size){ 
    char temp; 
    int i = 0; 
    int j = size-1; 

现在,i是0和j是3所以你交换物品0和3嘛,有什么项目?

0 = b 
1 = a 
2 = r 
3 = \0 

当您交换项目0和3时,将创建一个以终结符“\ 0rab”开头的字符串。当然,打印出来不会产生任何东西。

+0

这是一个以空字符结尾的字符串数组,它是一个c字符串。我很抱歉没有提到这一点。 – FluffyKittens 2014-11-06 04:34:24

+0

@DracoM。问题的关键在于你想要颠倒空终止符之前的字符(而不是将终止符包含在反转中) – 2014-11-06 04:37:20