user2877477
user2877477

Reputation: 263

Convert string to char for ascii

I want to ask for word from the user and then convert the word from string to char using 'strcpy'. Then I want to determine the sum of the ascii codes for all of the letters in the word.

However, I am having difficulties. I don't understand exactly how I can do that. This is what I have been able to do so far.

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

using namespace std;

int main()
{
    string word;
    cout << "Enter word: ";
    getline(cin, word);
    /*
        char w[word];
        strcpy(w,word.c_str());
        int ('A');
        cout<<char(65); 
    */
    return 0;
}

The commented part is where I have been trying to do the converting. I copied the code from a worksheet. Even if it did work, I don't know how, and what it all means.

Thanks for your help.

Upvotes: 0

Views: 1660

Answers (2)

David G
David G

Reputation: 96845

char w[word];
strcpy(w, word.c_str());

char w[word] is incorrect. The square brackets is for the size, which must be a constant integral expression. word is of type std::string, so this makes neither logical nor practical sense. Maybe you meant it as:

char w = word;

But that still won't work because word is a string, not a character. The correct code in this case is:

char* w = new char[word.size() + 1];

That is, you allocate the memory for w using a char*. Then you use word.size() + 1 to initialize heap-allocated memory amounting to those bytes. Don't forget for the obligatory delete[] when you're finished using w:

delete[] w;

However, note that using raw pointers and explicit new is not needed in this case. Your code can easily be cleaned up into the following:

#include <numeric>

int main ()
{
    std::string word;

    std::getline(std::cin, word);

    int sum = std::accumulate(word.begin(), word.end(), 0);                    /*
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                    */

    std::cout << "The sum is: " << sum << std::endl;
}

Upvotes: 3

Crowman
Crowman

Reputation: 25936

You don't need to use strcpy() (or use a char * at all, for that matter), but this'll do your counting using a char pointer:

#include <iostream>
#include <string>

int main() {
    std::string word;

    std::cout << "Enter word: ";
    std::cin >> word;

    const char * cword = word.c_str();
    int ascii_total = 0;

    while ( *cword ) {
        ascii_total += *cword++;
    }

    std::cout << "Sum of ASCII values of characters is: ";
    std::cout << ascii_total << std::endl;

    return 0;
}

Output:

paul@local:~/src/cpp/scratch$ ./asccount
Enter word: ABC
Sum of ASCII values of characters is: 198
paul@local:~/src/cpp/scratch$

If you really do want to use strcpy(), I'll leave it as an exercise to you to modify the above code.

Here's a better way to do it, just using std::string (and C++11, and obviously presuming your system uses the ASCII character set in the first place):

#include <iostream>
#include <string>

int main() {
    std::string word;

    std::cout << "Enter word: ";
    std::cin >> word;

    int ascii_total = 0;
    for ( auto s : word ) {
        ascii_total += s;
    }

    std::cout << "Sum of ASCII values of characters is: ";
    std::cout << ascii_total << std::endl;

    return 0;
}

Upvotes: 0

Related Questions