#include "stdafx.h"
#include <iostream>
using namespace std;
void ChangeString(char str[])
{
int nStrlen = strlen(str);
if(nStrlen>45||nStrlen<=0)
{
return;
}
if(nStrlen >0)
{
int i = 0;
for(i = 0;str[i] != '\0';i++)
{
if(str[i]>='a'&&str[i]<='z')
{
str[i] = str[i] - 32;
}
else
continue;
}
str[i] = '\0';
}
}
int _tmain(int argc, _TCHAR* argv[])
{
//char* str = new char[500];
//str = "AbcdefGHIGKL";
//ChangeString(str);
//cout<<str<<endl;
//delete [] str; char str[] = "AbcdefGHIGKL";
ChangeString(str);
cout<<str<<endl;
return 0;
}用指针出错。出错语句
str[i] = str[i] - 32;
这几天心情烦躁,压力大啊。

解决方案 »

  1.   

    #include <iostream>
    using namespace std;
    void ChangeString(char str[])
    {
    int nStrlen = strlen(str);
    if(nStrlen>45||nStrlen<=0)
    {
    return;
    }
    if(nStrlen >0)
    {
    int i = 0;
    for(i = 0;str[i] != '\0';i++)
    {
    if(str[i]>='a'&&str[i]<='z')
    {
    str[i] = str[i] - 32;
    }
    else
    continue;
    }
    str[i] = '\0';
    }
    }
    void main()
    {
    //char* str = new char[500];
    //str = "AbcdefGHIGKL";
    //ChangeString(str);
    //cout<<str<<endl;
    //delete [] str; char str[] = "AbcdefGHIGKL";
    ChangeString(str);
    cout<<str<<endl;

    }
    调试通过,没问题。。
      

  2.   

    #include <iostream>
    using namespace std;
    void ChangeString(char str[])
    {
    int nStrlen = strlen(str);
    if(nStrlen>45||nStrlen<=0)
    {
    return;
    }
    if(nStrlen >0)
    {
    int i = 0;
    for(i = 0;str[i] != '\0';i++)
    {
    if(str[i]>='a'&&str[i]<='z')
    {
    str[i] = str[i] - 32;
    }
    else
    continue;
    }
    str[i] = '\0';
    }
    }
    int main(int argc, char* argv[])
    {
    char* str = new char[500];
    strcpy_s(str,500,"AbcdefGHIGKL"); //这样才是赋值
    //str = "AbcdefGHIGKL";
    ChangeString(str);
    cout<<str<<endl;
    delete [] str; //char str[] = "AbcdefGHIGKL";
    //ChangeString(str);
    //cout<<str<<endl;
    return 0;
    }
      

  3.   

    char* str = "AbcdefGHIGKL"; //字符串赋值也可以这样,只不过这样 就是const值了
      

  4.   

    //char* str = new char[500];
        //str = "AbcdefGHIGKL";
    =========
    指针是一个地址,你这样写当然出错