T
T
The_Beginer2020-06-18 13:45:44
C++ / C#
The_Beginer, 2020-06-18 13:45:44

How to change a letter in a string to uppercase and vice versa?

The program must change the characters in the string. If it is a capital letter then it must be lowercase and vice versa. Use the cctype functions.
I loop through the entire line and using the conditional operator and the toupper, tolower functions I try to replace the characters in the line, but it doesn’t work (as you can see in the figure below, he writes numbers, and then we output the line but without changes)

5eeb45c4ccb89705096288.jpeg

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>

const int strSize = 100;

using namespace std;


int main()
{
  setlocale(LC_ALL, "Russian");
  char ch[strSize];
  char ch1[strSize] = { 0 };
  int itar=0;
  char *ptr=ch1;
  cout << "Введите строку:\n";
  cin >> ch;
-----//Рабочий код, не выкладываю///-------
  cout << "Преобразованная строка:"<<ch1<< endl;
  cout << "Работаем с регистрами" << endl;
  for (int i = 0; i < strlen(ch1); i++)//Прохожу всю строку циклом
  {
    if (tolower(ch1[i])) //Если буква маленькая выводим вместо неё большую
      cout<<toupper(ch1[i]);
    if (toupper(ch1[i]))
      cout<<tolower(ch1[i]); //Если буква большая выводим маленькую букву
  }
  cout << ch1 << endl;//Вывод строки?

  system("pause");
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Zhilin, 2020-06-18
@The_Beginer

tolowergives out the converted character, and in the form of int (horror!), you must immediately manually cast it to char. This is what needs to be brought out.
islowerchecks if the argument is a lowercase letter and also returns an int (horror!), it must be manually cast to bool immediately. This is what needs to be checked in if.
The fact that the program compiles at all is the "merit" of the authors of these functions, at that time types were not put into anything.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question