string array

This is a discussion on string array within the C++ Programming forums, part of the General Programming Boards category; how do i increment a string array? like a becomes b, x becomes y? ex. Hello world becomes ifmmp xpsme....

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    10

    string array

    how do i increment a string array?
    like a becomes b, x becomes y?

    ex. Hello world becomes ifmmp xpsme.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,655
    An array of chars is actually an array of 1 byte numbers.
    The ASCII table lists the character representations of these numbers. If you don't have that chart handy, just look for yourself:
    char c = 0;
    while(c < 127) {
    printf("%c = %i \n", c, c);
    getch();
    c++;
    }

    So really, it's just a matter of looping through the string and incrementing the value of each char. That's basically it.

  3. #3
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    If you are using the STL, you can do it with std::string and the transform algorithm:

    Code:
    #include <string>
    #include <algorithm>
    #include <iostream>
    using namespace std;
    
    class IncrementString {
    public:
    
        inline char operator()(char c){ return ++c; }
    
    };
    
    int main()
    {
        string str("Hello World");
    
        cout<<"String before transform: "<<str<<endl;
    
        transform(str.begin(), str.end(), str.begin(), IncrementString());
    
        cout<<"String after transform: "<<str<<endl;
       
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    10

    string array

    how about any word inputted in the c++ program? just any other word becomes all the letters in the word incremented

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21