C-String backwards/forwards

[red]
I got the program to print (on the screen) the message backwards. However, now that it is saved as, int len, it is now a backward message. I need to get the message saved correctly after this part so now I may find a certain letter and replace it with another letter, but first I need to reverse the message back.

HERE'S WHAT I HAVE !!!! I thought by calling the message again that I would be able to reverse the message but it prints out backwards still. Here's what I need this to do.

When I type: give me the knowledge i need

1st I get: [blue]deen i egdelwonk eht em evig [/blue]

which is correct.

I need now to have it save as: [blue] give me the knowledge i need[/blue]
This printed out so I know it did reverse.
[/red]

#include
#include
using namespace std;

void main ()
{
char name [80];
cout << "Enter saying: ";
cin.getline(name, 80);
int len = (int) strlen(name)-1;
for(int i=len; i>=0; i--)
{
cout << name[i];
}
cout<<endl;

(int) strlen(name)-1;
for (int i=len; i>=0; i--)
{
cout<<name[i];
}
cout<<endl;
}

Comments

  • [code]:
    : #include
    : #include
    : using namespace std;
    :
    : void main ()
    : {
    : char name [80];
    : cout << "Enter saying: ";
    : cin.getline(name, 80);
    [red]The loop below prints the string backwards as required.[/red]
    : int len = (int) strlen(name)-1;
    : for(int i=len; i>=0; i--)
    : {
    : cout << name[i];
    : }
    : cout<<endl;
    [red]The loop below also prints the string backwards -- just like the
    loop above. If you want to print the string forwards you need to
    change it so that the loop counts from 0 to the length of the string[/red]
    : (int) strlen(name)-1;
    : for (int i=len; i>=0; i--)
    : {
    : cout<<name[i];
    : }
    : cout<<endl;
    : }
    [/code]:

  • [red]
    Here's my code that works for reversing and reversing again my text. Now I need a code which I have not found that will search for text (s) and will replace that text with (t) and if it doesn't find the text it will return NULL. [italic] THANKS FOR THE HELP BEFORE STOBER[/italic][/red]

    #include
    #include
    using namespace std;

    void main ()
    {
    char name [80];
    cout << "Enter saying: ";
    cin.getline(name, 80);
    int len = (int) strlen(name)-1;
    for(int i=len; i>=0; i--)
    {
    cout << name[i];
    }
    cout<<endl;


    for (int i=0; i<80; i++)
    {
    cout<<name[i];
    }
    cout<<endl;

    char s, t;
    cout<<"What letter would you like to change"<<endl;
    cin >> s;
    cout<<"The letter you decided to change is "<< s <<endl;

    cout<<"What letter would you like to replace it with"<<endl;
    cin >> t;
    cout<<"The letter you have decided to change the letter with is "<< t << endl;


    }
  • [b][red]This message was edited by stober at 2003-12-1 13:39:42[/red][/b][hr]
    : [red]
    : Here's my code that works for reversing and reversing again my text. Now I need a code which I have not found that will search for text (s) and will replace that text with (t) and if it doesn't find the text it will return NULL. [italic] THANKS FOR THE HELP BEFORE STOBER[/italic][/red]
    :
    [blue]use strchr() to locate a specific character in the string. See [red]RED[/red] below for other suggested change.[/blue]
    [code]
    : #include
    : #include
    : using namespace std;
    :
    : void main ()
    : {
    : char name [80];
    : cout << "Enter saying: ";
    : cin.getline(name, 80);
    : int len = (int) strlen(name)-1;
    : for(int i=len; i>=0; i--)
    : {
    : cout << name[i];
    : }
    : cout<<endl;
    :
    :
    : for (int i=0; i<[red]len+1[/red]; i++)
    : {
    : cout<<name[i];
    : }
    : cout<<endl;
    :
    : char s, t;
    : cout<<"What letter would you like to change"<<endl;
    : cin >> s;
    : cout<<"The letter you decided to change is "<< s <<endl;
    :
    : cout<<"What letter would you like to replace it with"<<endl;
    : cin >> t;
    : cout<<"The letter you have decided to change the letter with is "<< t << endl;
    :
    :
    : }
    [/code]:



  • [red]
    I'm trying to replace a letter with another using this strchr() function, but the there is an error which I don't understand and maybe someone can find out what its telling me and if I'm doing this correctly.
    [/red]

    #include
    #include
    using namespace std;

    void main ()
    {
    char name [80];
    cout << "Enter saying: ";
    cin.getline(name, 80);
    int len = (int) strlen(name)-1;
    for(int i=len; i>=0; i--)
    {
    cout << name[i];
    }
    cout<<endl;


    for (int i=0; i<len+1; i++)
    {
    cout<<name[i];
    }
    cout<<endl;

    char s, t;
    cout<<"What letter would you like to change"<<endl;
    cin >> s;
    cout<<"The letter you decided to change is "<< s <<endl;

    cout<<"What letter would you like to replace it with"<<endl;
    cin >> t;
    cout<<"The letter you have decided to change the letter with is "<< t << endl;

    char* strchr(const char* name, char* s);

    char*s=char*t [red] do need an "if statement here"[/red]

    }


  • [code]:
    : #include
    : #include
    : using namespace std;
    :
    : void main ()
    : {
    : char name [80];
    : cout << "Enter saying: ";
    : cin.getline(name, 80);
    : int len = (int) strlen(name)-1;
    : for(int i=len; i>=0; i--)
    : {
    : cout << name[i];
    : }
    : cout<<endl;
    :
    :
    : for (int i=0; i<len+1; i++)
    : {
    : cout<<name[i];
    : }
    : cout<<endl;
    :
    : char s, t;
    : cout<<"What letter would you like to change"<<endl;
    : cin >> s;
    : cout<<"The letter you decided to change is "<< s <<endl;
    :
    : cout<<"What letter would you like to replace it with"<<endl;
    : cin >> t;
    : cout<<"The letter you have decided to change the letter with is "<< t << endl;

    [red]The next two lines are all wrong. What you want to do is use
    strchr() to find the first occurrence of the character s in the string
    name. So, if you read the descriptkon for strchr() carefully and read
    the example program for it, it works like this:

    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wcecrt/htm/_wcecrt_strchr_wcschr.asp

    [blue] char *ptr = strchr(name,s); [/blue]

    At this point, ptr points to the first occurrence of the character s in
    name, or NULL if it does not exist. Now all you have to do is to
    replace the character with t.

    [blue] if(ptr != NULL) *ptr = t;[/blue]
    [/red]
    :
    : }
    :
    :
    [/code]:

  • [b][red]This message was edited by gr7221 at 2003-12-2 19:40:8[/red][/b][hr]
    [red]
    I need all the occurrences of the letter not just the first. I tried putting a "for" statement in but it doesn't continue. WHY??[/red]

    [code]
    #include
    #include
    using namespace std;

    void main ()
    {
    char name [80];
    cout << "Enter saying: ";
    cin.getline(name, 80);
    int len = (int) strlen(name)-1;
    for(int i=len; i>=0; i--)
    {
    cout << name[i];
    }
    cout<<endl;


    for (int i=0; i<len+1; i++)
    {
    cout<<name[i];
    }
    cout<<endl;

    char s, t;
    cout<<"What letter would you like to change"<<endl;
    cin >> s;
    cout<<"The letter you decided to change is "<< s <<endl;

    cout<<"What letter would you like to replace it with"<<endl;
    cin >> t;
    cout<<"The letter you have decided to change the letter with is "<< t << endl;

    char *ptr = strchr(name,s);

    if(ptr != NULL)
    {
    for( int i=0; i<=len; i++)
    {
    *ptr = t;
    }
    }

    else if(ptr==NULL)
    cout<<"Letter was not found
    ";

    for (int i=0; i<len+1; i++)
    {
    cout<<name[i];
    }
    cout<<endl;

    }
    [/code]

    [red]
    I need all the occurrences of the letter not just the first. I tried putting a "for" statement in but it doesn't continue. WHY??[/red]


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories