简体   繁体   English

使用指针反转C ++中的字符数组

[英]Reversing an array of characters in C++ using pointers

So the program is supposed to use a loop to count the total size of the array of characters that contains "Hello World!", return that count as an int, and then call a function that uses pointers to start at the beginning and end of the array to swap the characters until the null point " " in between the words. 因此,该程序应该使用循环来计算包含“ Hello World!”的字符数组的总大小,以int形式返回该计数,然后调用一个使用指针从函数的开头和结尾开始的函数交换字符的数组,直到单词之间的空点“”。 I have largely figured out the code, but it won't compile correctly and won't display or won't complete the swap, I keep getting the following error: "LNK1168 cannot open Lab06.exe for writing" 我已经在很大程度上弄清楚了代码,但是它无法正确编译,无法显示或无法完成交换,我不断收到以下错误:“ LNK1168无法打开Lab06.exe进行写入”
How can I fix this? 我怎样才能解决这个问题?

#include "targetver.h"
#include <iomanip>
#include <array>
#include <stdio.h>
#include <tchar.h>
#include <string>
using namespace std;

int main()
{
    //int to hold size of array
    int count = 0;
    // declare a c-string to reverse
    char myString[] = "Hello world!";
    //print array as is
    cout << myString << endl;
    //find size of the array


    // call the reverser function
    reverser(myString, count);

    // output the result
    cout << myString << endl;

    system("PAUSE");
    return 0;
}

int findSize(char myString[], int count)
{

    count = (unsigned)strlen(myString);
    cout << "The size of the Array is: " << count << endl;
    return count;

}


void reverser(char myString[], int count)
{
    findSize(myString, count);
    char *strPtr;
    char *endPtr;
    endPtr =& myString[count];
    strPtr = myString;

    for (int x = 0; x <= count; x++)
    {
        strPtr++;
        *strPtr = myString[x];
        endPtr--;
        *endPtr = myString[count--];
        *strPtr = *endPtr;
        cout << strPtr << endl;
        cout << endPtr << endl;
    }

}

OK lets start at the top 好,让我们从顶部开始

findsize returns the size, but you are ignoring it. findsize返回大小,但是您忽略了它。 You try to pass in count as an argument and hope to update count in findsize. 您尝试将count作为参数传递,并希望更新findsize中的count。 This isnt how C works. 这不是C的工作方式。 So first do 所以先做

int findSize(char myString[])
{

    int count = strlen(myString);
    cout << "The size of the Array is: " << count << endl;
    return count;
}

now when you call findsize you must remember the result. 现在,当您调用findsize时,您必须记住结果。

int count = findsize(myString);

This return a count of the length. 这将返回一个长度计数。 Say the string is 'abc', count will be 3. 假设字符串为“ abc”,计数为3。

The string has 3 chars myString[0], mystring[1] and ystring[2]. 该字符串有3个字符,myString [0],mystring [1]和ystring [2]。 Note that there is no [3] character.. 请注意,没有[3]字符。

Reversing in place is tricky. 逆向转换很棘手。

void reverser(char myString[])
{
    int count = findSize(myString);
    char *strPtr = myString
    char *endPtr = &myString[count - 1];
    strPtr = myString;

    for (int x = 0; x <= count / 2; x++)
    {
       char swap = *strPtr;
       strPtr[x] = *endPtr;
       *endPtr = swap;
       endPtr--;
    }
}

Disclaimer - I have not tested this code. 免责声明-我尚未测试此代码。

count = findSize(myString, count);

since findSize returns count you need count to equal the value it returns. 由于findSize返回count,因此您需要count使其等于返回的值。

void reverser(char myString[])
{
int count = findSize(myString);
char *endPtr;
endPtr =& myString[count-1];
char *temp = new char[count];

for (int x = 0; x < count; x++)
{
     temp[x] = *endPtr;//this assgins the last letter of myString to the first element of temp 
     endPtr--;//decrement so endPtr points to myString[count - 1];
}
//now transfer
for(int x = 0; x < count; x++){
    myString[x] = temp[x];
}
delete [] temp;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM