1

this question should be easy and straight forward, but after searching online, I couldn't find an answer. might because the question is just too simple.
following code is from cplusplus.com. it's a function of making a string lowercase. I was intended to do something similar.

/* tolower example */
#include <stdio.h>
#include <ctype.h>
int main ()
{
  int i=0;
  char str[]="Test String.\n";
  char c;
  while (str[i])
  {
    c=str[i];
    putchar (tolower(c));
    i++;
  }
  return 0;
}

and what I made is this:

void search(string A[], string B[], int k)
{                               
    int temp;
    for(int j = 0; j <= 4; j++)
    {
        for(int i = 0; i <= k; i++)
        {
            string str (A[i]);
            int h = 0;
            char lstr[] = B[j];
            char c;
            while (lstr[h])
            {
                c = lstr[h];
                putchar (tolower(c));
                h++;
            }
            string key (B[j]);
.....

this part of the code is in a for loop. B[j] is a string array.
Visual Studio informed me that char lstr[] = B[j]; part is not right, the error message is:
Error: initialization with '{...}' expected for aggregate object.

I think the problem is that I didn't use the correct syntax of using a string array in a function. something should be done for B[j], in order to make it a char array. I couldn't figure it out.
is that something about pointer? sorry I haven't learn pointer yet.

does my question make sense for you? any help is greatly appreciated!!

5
  • can you include the declaration of B[j]? Commented Jun 11, 2013 at 22:10
  • @keelar is that enough for you? I know it's not pretty. Thank you for help! Commented Jun 11, 2013 at 22:16
  • You're using string objects in your example, and the original code from cpluspuls utilizes character arrays. Which are you do you want to work with? Commented Jun 11, 2013 at 22:19
  • @ryanbwork, I do want to work with string objects that string array. Thanks! Commented Jun 11, 2013 at 22:23
  • See the answers below on how to get the character array representation of a string object. Commented Jun 11, 2013 at 22:26

3 Answers 3

1

If you're looking to make the letters in the string lowercase it's more readable to just work with strings all the way and use std::transform. For example,

// make sure to #include <algorithm>
// at the top
string lstr = B[j];
std::transform(lstr.begin(), lstr.end(), lstr.begin(), ::tolower);

This is much more natural and c++ idiomatic than working with char * directly and less error-prone.

Sign up to request clarification or add additional context in comments.

1 Comment

Remember you can accept and/or upvote answers that you found helpful.
1

You're trying to assign a char to char[]. You can get the effect you want with the following code:

....
int h = 0;
char* lstr = &B[j]; // point lstr to the address of j'th element of B.
char c;
while (lstr[h])
{
    c = lstr[h];
    putchar (tolower(c));
    h++;
}
.....

What this does is that lstr is now a pointer that points to the j'th character in B. Arrays are essentially pointers. When you do B[j], it's equivalent to writing char ch = *(B + j);, where B points to the address of the first character in the array of characters (otherwise known as string).

EDIT After your edit, it now seems that you're trying to assign a std::string to a char. Here is the corrected solution.

....
int h = 0;
string& lstr = B[j]; // grab a reference to the j'th string in B.
char c;
while (lstr[h])
{
    c = lstr[h];
    putchar (tolower(c));
    h++;
}
.....

Here, lstr is essentially a reference to the j'th string in B and you can use it as a regular string just like how you're using string str(A[i]);, which makes a copy of the i'th string in A.

1 Comment

After seeing the definition of B[], your answer is incorrect though the upvote still remains.
1

You're confusing character arrays and string objects here. A character array is an array of bytes of set size which is null terminated, while a string is an object which expands/contracts as is necessary and doesn't require the null terminator. You're attempting to assign a string object to a character array, which is unsupported. If you're working with string objects, and want to retrieve their equivalent character array, utilize the c_str() function:

const char* lstr = B[j].c_str()

Also, utilizing an array name of B and an index of j is hilarious.

3 Comments

Thank you so much for the explanation!! that does help a lot! I'm not sure why this happen, when I change it; VS marked B in this statement and showed: Error: a value of type "const char *" cannot be used to initialize an entity of type "char *" Thank you so much for help sir!
Sorry about that. c_str() returns a pointer to array, and that array cannot be modified. You either need to cast the output of the c_str() to a char* (IE (char*)B[j].c_str()) or utilize a const char*.
Yes, i-j-k are used frequently in loops. Utilizing a more verbose naming scheme will be helpful in the future so you don't confuse yourself, but for now its fine. I was just being immature ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.