Home > OS >  C can't return nth element of string only nth element onwards
C can't return nth element of string only nth element onwards

Time:10-12

I am trying to get the nth element of a string, which I know generally works with a method such as

char current_letter = input_string[j];

However when using the code below I get an output of

0 current_letter: ABC
1 current_letter: BC
2 current_letter: C
ABC BC 9ekNc5GlorW1PkaBQlYCuXMBljdSQClygl00XwKxoVzYsf8FrCs3qUZV85gHJHsl

where the 4th line is just the coded_string variable being printed. This indicates that the std::string current_letter = &input_string[j]; way of trying to get the nth element of the string is not actually returning the nth element but rather everything in the string from the specified index onward.

When I use char instead of std::string to define the current_letter variable (and correspondingly change it from std::string to char in the previous lines defining the std::map and the .insert() method to match, I get an red error line under the .find() method, saying:

no instance of overloaded function "std::map<_Key, _Tp, _Compare, _Alloc>::find [with _Key=std::string, _Tp=std::string, _Compare=std::less<std::string>, _Alloc=std::allocator<std::pair<const std::string, std::string>>]" matches the argument list

I would also like to generally be able to use std::string for both the key and value in the std::map for future purposes, so how would I retrieve the nth letter of the string without any other element in the string being returned?

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <string>

void set_seed_n_shuffle(std::vector<std::string> &array, unsigned seed){
    std::srand(seed);
    random_shuffle(std::begin(array), std::end(array));

}

std::string en_to_ch(std::vector<std::string> list, std::string input_string){
    // list = ch_list, input_string = string to be converted

    std::vector<std::string> en_list = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J"};

    std::map<std::string, std::string> en_ch_dict;

    for (int i=0; i<en_list.size(); i  ){

        en_ch_dict.insert(std::pair<std::string, std::string>(en_list[i], list[i]));
        //en_ch_dict.emplace(en_list[i], list[i]);
    }

    std::string coded_string;

    for (int j=0; j<input_string.size(); j  ){
        std::string current_letter = &input_string[j];
        std::cout << j << " current_letter: " << current_letter << "\n";
        if (en_ch_dict.find(current_letter) == en_ch_dict.end()) {
            coded_string  = current_letter   " "; // not found
        } else {
            coded_string  = en_ch_dict.find(current_letter)->second   " "; // found
        }


    }

    return coded_string;
}

int main(){
    std::vector<std::string> ch_list = {"randomcharacters1", "randomcharacters2","randomcharacters3","randomcharacters4","randomcharacters5",}

    set_seed_n_shuffle(ch_list, 156);
    
    std::string string_input = "ABC";

    std::string new_string = en_to_ch(ch_list, string_input);

    std::cout << new_string << "\n";

}

CodePudding user response:

    std::string current_letter = &input_string[j];

input_string[j] is a single character, not a null-terminated string. If you construct a std::string with a char* pointer to a character, it will read from that character onward until it encounters a null terminator '\0', which doesn't exist in this case, so the result is undefined behavior.

  • Related