Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <iostream>

using namespace std;

char chng(char a){
    int left,right,i=0,count=0;
    char temp,output;
    left=0;
    while(a[i]!='\0'){

        count++;
        i++;

    }
    right=count-1;
while(left<right){ if(!(a[left]="">='a'&& a[left]<='z')){
            left++;
        }

  else if(!(a[right]>='a'&& a[right]<='z')){

                right--;
                }

        else{
            temp=a[left];
            a[left]=a[right];
            a[right]=temp;

        }
    left++;
    right--;

}
    output = a;
    return output;

}

int main()
{
    char b;
    cin>>b;
    chng(b);
    return 0;
}


What I have tried:

the problem is:
char to char*
int char problem
how to pass pointer string stuffs
please explain
Posted
Updated 26-Oct-16 7:59am
v2
Comments
Richard MacCutchan 26-Oct-16 13:01pm    
You are trying to change a single character element which makes no sense. You need to create a character array and fill that first.

1 solution

This is a very confused bit of code you have there...
You declare a as a char value:
C++
char chng(char a){
Which means that you can only pass a single byte to the method.
But...you immediately start to use it as if it was an array of characters:
C#
while(a[i]!='\0'){
That's bad, for a lot of reasons, all of them centered around the fundamental array definition is that the name of an array is a pointer to the first element. Which mean that the system is trying to use your single byte value as a multibyte pointer - 4 bytes for a 32 bit system, 8 on a 64 bit). That isn't going to work, no matter what you do.
You are also trying to return a pointer as a single character, which isn;t goign to work either.

So start by changing your function declaration:
C#
char* chng(char* input){
(Don't use single character names, it makes it a lot harder to read your code later - descriptive names for all your variables helps your code become self documenting).

Then change main to get a string instead of a character from your user, and pass that to the function. When you have that working, write another function which you pass a string and two integers, which reverses all the characters between the two indexes you pass. Use that to reverse the whole string for testing and print the result in your main method. When that all works, start working on leaving the special characters alone.

Doing it all in small stages like this means that each time you are only doing a little bit extra work, but that you know that everything it's based on is working. It may seem slower, but it gets to the solution a lot, lot faster than trying to write the whole thing at once and finding that something doesn't work!


Reply:
C++
#include <iostream>

using namespace std;

char* chng(char* a){
    int left,right,i=0,count=0;
    char *temp,output;
    left=0;
    while(a[i]!='\0'){

        count++;
        i++;

    }
    right=count-1;
while(left<right){ if(!(a[left]="">='a'&& a[left]<='z')){
            left++;
        }
        else if(!(a[right]>='a'&& a[right]<='z')){

                right--;
                }
        else{
            *temp=a[left];
            a[left]=a[right];
            a[right]=*temp;

        }
    left++;
    right--;

}
    output= *a ;
    return output;

}

int main()
{
    char* b;
    cin>>b;
    cout<<chng(b);
    return 0;
}


Where do I start ...

Let's start at the beginning, because until you get the basics right, nothing else can be expected to work.
C++
int main()
{
    char* b;
    cin>>b;
    cout<<chng(b);
    return 0;
}
Declaring a variable as char* doesn;t allocate any memory for your user to type into: so where is the data you read with cin supposed to go?
Remove the call to chng for a moment, and just get main working first - use cout to echo the user input back to him, and get that working.
I'm pretty sure you know how, or have been told how: this isn't "hello world" so user input is something you will have done before. Stop guessing what you need to do, and think about it - this isn't complicated!
 
Share this answer
 
v2
Comments
Member 12817247 29-Oct-16 11:13am    
so how by using char* chng(char* input){........ I can enter a string??
OriginalGriff 29-Oct-16 11:16am    
By supplying a pointer to a character (or the name of an array of characters, it's the same thing) to the cin call, and passing that to your function.
You've almost certainly done it before in earlier exercises!
Member 12817247 29-Oct-16 12:00pm    
#include <iostream>

using namespace std;

char* chng(char* a){
int left,right,i=0,count=0;
char *temp,output;
left=0;
while(a[i]!='\0'){

count++;
i++;

}
right=count-1;
while(left<right){ if(!(a[left]="">='a'&& a[left]<='z')){
left++;
}
else if(!(a[right]>='a'&& a[right]<='z')){

right--;
}
else{
*temp=a[left];
a[left]=a[right];
a[right]=*temp;

}
left++;
right--;

}
output= *a ;
return output;

}

int main()
{
char* b;
cin>>b;
cout<<chng(b);
return 0;
}




i did this ...but the "return output" is not working
OriginalGriff 29-Oct-16 12:06pm    
Answer updated.
Philippe Mori 30-Oct-16 19:05pm    
The line cin>>b; would be incorrect as the pointer is not initialized.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900