So uh, i need some help with these functions..i gotta reverse, but i do not have the functions that can make it lowercase, or remove punctuation

#include <iostream.h>
#include <string.h>

char your_string[256];
char new_string[256];
void ReverseString();

void main()
{
    cin.getline(your_string, 256);
    ReverseString();
    cout << ReverseString(your_string) << "\n";
}
void LowerCase(char String){
     
void ReverseString(){
    int x = strlen(your_string);
    x--;
    for(int y = x; y >= 0; y--){
     your_string[y] += your_string[y];
    }
  }

Recommended Answers

All 11 Replies

i could do this with my hands tied behind my back and plunk it out with my nose it's so easy. what a noob.

commented: Don't be so unkind and unhelpful. -6
commented: ad ++ -1

That was a hugely unhelpful reply, I apologize knewc.

now, as for your question. to make things lowercase I recommend looking into the function "tolower"

As for the removing punctuation, replace() should do the trick. Here's the links to the functions:

http://www.cplusplus.com/reference/clibrary/cctype/tolower.html

http://www.cplusplus.com/reference/string/string/replace.html

Also, line 12 should give you an error as you are passing an argument into a function that doesn't take arguments. But your function should take that argument as otherwise it wouldn't know what string to reverse
(and if it did work, wouldn't it change it back to it's original state? reversing it twice)

knewc: line 20: you can't reverse the string using the same charcter array as the original string. You need to use a second array. That's the purpose of new_string. And use the = operator, not +=.

>>i could do this with my hands tied behind my back and plunk it out with my nose it's so easy. what a noob.
You was a newbe once too, so don't laugh too loudly.

knewc: line 20: you can't reverse the string using the same charcter array as the original string. You need to use a second array. That's the purpose of new_string. And use the = operator, not +=.

You can reverse a string in place by exchanging the characters at each end, working toward the middle. You do need a one char temporary holding spot as you do the three-way swap operation. You use separate index variables for each end, being sure to stop when you reach the mid-point.

commented: Thanks for the correction :) +28

You can reverse a string in place by exchanging the characters at each end, working toward the middle. You do need a one char temporary holding spot as you do the three-way swap operation. You use separate index variables for each end, being sure to stop when you reach the mid-point.

Oh yea -- that's one reason you teach and I don't :)

Oh yea -- that's one reason you teach and I don't :)

This is one of my favorite student-torturing problems ;)

Some compilers (Visual C++ especially) have a function that does just that. I've not found much real life justification for it, so I just explain it's there to provide a model for students to recreate, as we do with many of the other string functions.

knewc - are you making any progress?

this is where i'm at so far...I need a function that tokenizes and removes punctuation using * string type (I have no clue how to do that).

#include <iostream.h>
#include <string.h>
using namespace std;

char String[256];
char NewString[256];
char ReverseString(char String, char NewString);
char LowerCase(char String);
char RemovePunct();

int main()
{
    cin.getline(String, 256);
    cout << "Here is your string: " << String << endl;
    ReverseString();
    cout << ReverseString(String) << "\n";
    LowerCase();
    cout << LowerCase(String) << endl;
    RemovePunct();
    cout << RemovePunct(String) << endl;
  }
char LowerCase(char String){
   if (String >='A' && String<='Z')
   String = (String('A'-'a')++); 
  }

char RemovePunct(char * String){
     char * New;
     New = strtok(String,",.-;!?");
     while(New != Null){
        New = strtok(Null, " ,.-;!?");
        return String;
        }
     }
     
char ReverseString(char String){
    int x = strlen(String);
    x--;
    for(int y = x; y >= 0; y--){
    String[y] = NewString[y];
    return NewString;
    }
  }

Your RemovePunct() only destroys the original string because strtok() inserts null bytes which renders the original string useless. You will first need to create another character array in which you can copy/concantinate the strings found by strtok(). Just mearly calling strtok() does not actually do anything but return a pointer to the beginning of the string that is terminated with one of those punctuation characters.

char tmpbuf[255] = {0};
char* New = strtok(String,",.-;!?");
while( New != NULL)
{
     strcat(tmpbuf,New);
     New = strtok(NULL,",.-;!?"); 
}
// now tmpbuf contains the final string without punctuation

The function ReverseString() is also incorrect. You need two counters there -- one that counts 0 to length and the other count down length to 0. Line 40 does nothing but copy the original string in the same order -- it does not reverse the string.

A clarification is needed - is your remove punctuation supposed to delete the punctuation and close up the gap, or simply replace any with a blank?

And, for functions of this type, you don't need to return the string, since you're passing it by reference.

Your lower case function needs to walk through the string, modifying each character in turn, if needed, by subtracting the difference ('A'-'a') from each char.

In the reverse function, the two counters should only go till they meet.

line 22: String is only declared as a single character -- it needs to be an array
char LowerCase(char String[]){

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.