Changing all characters in string to ASCII value of one up...help!

This is a discussion on Changing all characters in string to ASCII value of one up...help! within the C++ Programming forums, part of the General Programming Boards category; I need to make a program that changes all the ASCII values of the characters in a string to one ...

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    13

    Changing all characters in string to ASCII value of one up...help!

    I need to make a program that changes all the ASCII values of the characters in a string to one ASCII value above the ones in the string, so "ABCDEF" would become "BCDEFG" and "apple" would become "bqqmf", for examples. This is what I have so far, and it's way off:

    Code:
    // PROJECT 10-2.CPP
    // By Josh Milewski
    
    #include<iostream>
    using namespace std;
    
    main()
    {
    	const int size = 21;
    	char x[size];
    	cout << "Input a string: ";
    	cin.get(x,size);
    	for(char y = x[0]; y < x[20]; y = y++)
    	{
    		cout << y++;
    	}
    	cout << '\n';
    	return 0;
    }
    Please help me! Thank you very much!

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    Because you using ASCII, just add 1 to each string character.
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
        string s;
    
        cout << "Enter a string> ";
        if (!getline(cin, s)) {
            cerr << "Input failure" << endl;
            return 1; // Ren is still lazy
        }
        for (string::size_type i = 0; i < s.length(); ++i)
            ++s[i];
        cout << s <<endl;
    
        return 0;
    }

  3. #3
    C Programmer Stack Overflow's Avatar
    Join Date
    Apr 2004
    Posts
    477
    Hello,

    The word "apple" is 97 112 112 108 101 in ASCII. A good reference is ASCIITable.com. So for example, 'A' is a character constant; in the ASCII character set its value is 65, the internal representation of character A. If this makes sense, we can increment the value of an array subscript by using the ++, increment, operator.

    For example, take a character array word[3] and store "Hi". Logically, word[0] will be 'H' which is 72 in ASCII. If we increment the value of the array subscript, we can modify the internal representation:
    Code:
    ++word[0]; // Increment value
    To modify each index you can create a for loop that loops from 0 to the length of your string. During each loop you can execute the code to increment your variable subscript.

    Note: [Ren] showed an example of how to do it.


    - Stack Overflow
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    You loop need to go from index 0 up to (not including) index of the length of the string. The length of the string is NOT necessarily the size of the character array. Unless you want to loop through and find the NULL character in order to get the length, I'd suggest using the strlen function declared in the cstring header. Inside your loop, you should display the preincremented value of the character at the current index.

    Code:
    #include<iostream>
    #include <cstring>  // For the strlen function
    using namespace std;
    
    int main()
    {
        const int size = 21;
        int len;
        char x[size];
        cout << "Input a string: ";
        cin.get(x,size);
        len = strlen(x);
        for(int y = 0; y < len; ++y)
        {
            cout << ++(x[y]);
        }
        cout << '\n';
        return 0;
    }
    If you do not take into account the true length of the string (not the size of the character array), you will end up going through the whole array and incrementing possible junk values (including the NULL terminating character wherever it may happen to be in the array).

    Of course, the above simply displays the values stored in the array in a different (one up) form and does not actually change them to what is being displayed which was what your post said you wanted. To do that you will need to assign the new values to the old positions... Also what do you want to have happen with the 'Z' and 'z' characters?
    Last edited by hk_mp5kpdw; 12-22-2004 at 01:40 PM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Code:
    #include<iostream>
    using namespace std;
    
    main()
    {
    	const int size = 21;
    	char x[size];
    	cout << "Input a string: ";
    	cin.get(x,size);
    	for(char y = x[0];  y < x[20];  y = y++)
    	{
    		cout << y++;
    	}
    	cout << '\n';
    	return 0;
    }
    Your code is way off because you are using the highlighted variables in ways that you are not intending.

    You are initially setting your counter variable to "the value of the first element of the x[] array", and setting the condition of the for loop to be "as long as y is less than the value of the 21st element of the array x[]". (remember arrays start counting at 0, so 0 is the first element, and the index 20 is the 21st element). Next, in the for loop you are just incrementing the counter variable which does not change anything in the x[] array.

    Edit: See what happens when you take forever to type something while at work?

  6. #6
    Registered User
    Join Date
    Dec 2004
    Posts
    13
    Happiness has become me! Thank you so much, everybody! Here is the code I am using that works!

    Code:
    // PROJECT 10-2.CPP
    // By Josh Milewski
    
    #include<iostream>   // replaces crazy old iostream.h
    #include<cstring>   // needed for strlen function
    using namespace std;   // fixes namespacing issues in iostream
    
    int main()
    {
        const int size = 500;
        int len;
        char x[size];   // character called x of size 500
        cout << "Input a string: ";
        cin.get(x, size);   // gets x up to 500 characters
        len = strlen(x);   // len equals the length of x
        cout << "Encrypted: ";
        for(int y = 0; y < len; ++y)   // y is 0, the first character of x
        {                              // and as long as y is less than the length of x
            cout << ++(x[y]);          // the y of x plus one
        }                              // then adds one to y and starts over
        cout << '\n';
        system ("pause");
        return 0;
    }
    Again, thank you very much!
    Last edited by Josh7289; 12-22-2004 at 05:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-26-2003, 11:03 PM

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