Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
148 views
in Technique[技术] by (71.8m points)

How can I assign value to a string "char by char" after declaration in c?

int main(void)
{
   string original = "ABCdef";
   string upper;
   string lower;
   
   for (int i = 0; i < 6; i++)
   {
      if (isupper(original[i]) > 0)
      {
         upper[i]= original[i] - 32;
      }
      else
      {
         upper[i] = original[i];
      }
   }

   for (int i = 0; i < 6; i++)
   {
      if (islower(key[i]) > 0)
      {
         lower_key[i] = key[i] + 32;
      }
      else
      {
         lower_key[i] = key[i];
      }
   }
}
  

My goal here is to create 2 new strings, upper and lower, that are the uppercase and lowercase versions of original to be used later in my program. When I run the program as it is above the IDE tells me that I must assign upper and lower values before using them.

I have tried assigning NULL to them both at initialisation, as the IDE suggests, but this resulted in an error. When that failed, I tried assigning original to both. This almost worked, but for some reason meant that all three variables were altered when I assigned upper and lower different values within the if functions. This is a problem, as I need to be able to use upper and lower separately later on. Finally, I tried assigning "ABCDEF" to them both, but of course ran into the issue of an "incompatible pointer to integer assigning to 'string'" error.

How should I declare upper and lower in order for them to work as intended?

question from:https://stackoverflow.com/questions/65830649/how-can-i-assign-value-to-a-string-char-by-char-after-declaration-in-c

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...