Convert String to Uppercase Decussate in C++ - c++

I have this code:
Str UpperCase()
{
Str Result;
int i = 0;
for (; string[i]; i++)
{
if (string[i] <= 'z' && string[i] >= 'a')
{
string[i] -= 32;
}
Result.string[i] = string[i];
}
Result.string[i] = 0;
return Result;
}
It will make String Uppercase.
What should I do if I want it to be Decussate?
Example: hi my name is pooya ==> Hi My NaMe Is PoOyA
Sorry for my bad english
and Thanks ;)

Str UpperCase()
{
Str Result;
int i = 0;
int decussate = 0;
for (; string[i]; i++)
{
if (string[i] <= 'z' && string[i] >= 'a')
{
decussate++;
if( decussate%2 == 1 ){
string[i] -= 32;
}
}
Result.string[i] = string[i];
}
Result.string[i] = 0;
return Result;
}
Add int decussate, by changing it between an odd and an even number everytime a lowercase letter is found, it will create a pattern in which the 1,3,5,7,and so on letters will be capitalized, assuming the string is in lowercase.

Related

How to ignore white space and marks when encrypting a string variable in c++

I am trying to encrypt a string with white space and a question mark on it, for example, I am going to encrypt all the alphabet into numbers based on the order of the alphabet, but when I turn the string into numbers white space and question mark also gets enumerated how do I ignore that?
example)
int main() {
string alphabet;
int encrypted_code;
int first_int = 1, second_int = 2, third_int = 3;
string text = "hey what is your name?";
for (char ch = 'a'; ch <= 'z'; ch++) {
alphabet += ch;
}
for (int i = 0; i < text.length(); i++) {
encrypted_code = alphabet.find(text[i]);
if (i == 0 || i % 3 == 0) {
encrypted_code = encrypted_code + first_int;
}
cout << encrypted_code << " ";
}
}
Use an if statement to only work on letters:
for (int i = 0; i < text.length(); i++) {
if ('a' <= text[i] && text[i] <= 'z') {
// rest of your code goes here
}
}

How to captalize and title a string

If I was given a string, I need to either capitalize or title the string. For example:
There are some problems with your code. Here is the modified version of your code that works fine. Here:
std::string Capitalize(const std::string &str) {
std::string Ret;
for (int i = 0; i < str.length(); i++){
char c = str[i];
if (i == 0){
Ret += toupper(c);
}
else if (i != 0){
Ret += (tolower(c));
}
}
return Ret;}
Condition in for loop needs to be str.length() not Ret.length() and here :
std::string Title(const std::string &str) {
std::string Ret;
int i=0;
for (int i=0;i<str.size();i++) {
if(!(i==0 && str[i]==' '))
Ret += tolower(str[i]);
}
int size = Ret.length();
for (int i = 0; i < size; i++) {
if (i==0 || Ret[i - 1] == ' ')
{
Ret[i] = toupper(Ret[i]);
}
}
return Ret;}
Check if i is 0 to prevent out of range access to string.
Use a stringstream to first split all words, so that you can do this easily with a vector. This is an implementation of the Title function:
std::string Title(const std::string &str) {
std::vector<string>words;
words.clear();
std::string res = str, std::ans = "";
// It's better to pass the string AFTER you convert it all lowercase. Or you can only work with the capitalized characters:
for(int i = 0; i < res.size(); ++i){
if(res[i] >= 'A' && res[i] <= 'Z'){
res[i] = tolower(res[i]);
}
}
istringstream ss(res); // We push the modified string into a stringstream.
do{
res = "";
ss >> res;
words.push_back(res); // We split the string at " " and push each word in the vector.
} while(ss)
for(int i = 0; i < words.size(); ++i){
res = words[i];
res[0] = toUpper(res[0]); // For each word, we capitalize it
ans += res; // We add the word to our return string.
if(i < words.size() - 1){
ans += " "; // If this is not the last word, add a space
}
}
return ans;
}
As for the capitalization, you can do something like this:
std::string Capitalize(const std::string &&str){
std::string res = str;
res[0] = toupper(res[0]);
for(int i = 1; i < res.size(); ++i){
if(res[i] >= 'A' && res[i] <= 'Z'){
res[i] = tolower(res[i]); // converting if only an uppercase character.
}
}
return res; // If you pass a reference, the original will be modified so no return required.
}

convert lowercase to uppercase first character and make other chars stay lower

void correcter(string s, int j)
{
string correct;
for (; j < s.length(); j++)
{
if (int(s[j]) != 46){
if (int(s[j]) >= 97 && int(s[j]) <= 122 && i == 0)
{
char a = int(s[j]) - 32;
correct += a;
i++;
}
else if (int(s[j]) >= 65 && int(s[j]) <= 90&&i==0)
{
char a = int(s[j]) + 32;
correct += a;
i++;
}
else if (int(s[j]) >= 65 && int(s[j]) <= 90)
{
char a = int(s[j]) + 32;
correct += a;
i++;
}
else
correct += s[j];
}
else
{
correct += ". ";
i = 0;
}
}
cout << correct << endl;
}
question is to write a code that convert first character of string to uppercase and other stay as lowercase. after every "." make the words first char again upper and other parts in lower!
Input:
hellOWOrLD.hELLOWORLD.
Output:
Helloworld. Helloworld.
It should work like in the picture...
I would use isalpha(), toupper() and tolower()
EDIT: To look for punctuation.
#include <iostream>
#include <cctype>
#include <string>
using namespace std;
void upperCase(string& line) {
bool firstCharacter = true;
string punctuation=".?!";
for(int i = 0; line[i] != '\0'; i++) {
char c=line[i];
if(isalpha(c)) {
if (firstCharacter) {
line[i] = toupper(c);
firstCharacter = false;
} else {
line[i] = tolower(c);
}
} else if (punctuation.find(c)!=string::npos) {
firstCharacter=true;
}
}
}
int main() {
string str = "hello UNiverse?! World? Hello. Hello";
upperCase(str);
std::cout << str << '\n';
}

Reverse an array without affecting special characters

Is it possible to reverse an array without affecting the special characters ? By special characters, I mean anything characters not included from 'a' to 'z' and 'A' to 'Z'. I am short of ideas to build the algorithm, I still haven't it figured out.
One simple solution would be to Simple Solution:
1) Create a temporary character array --> ex: myArr[].
2) Copy alphabetic characters from the given array to myArr[].
3) Reverse myArr[] using standard string reversal algorithm.
4) Now traverse input string and myArr in a single loop. Wherever there is alphabetic character is input string, replace it with current character of myArr[].
Little problem with above solution, it requires extra space and it does two traversals of input string.
You can reverse with one traversal and without extra space. Below is algorithm.
1) Let input string be 'str[]' and length of string be 'a'
2) l = 0, r = a-1
3) While l is smaller than r, do following
a) If str[l] is not an alphabetic character, do l++
b) Else If str[r] is not an alphabetic character, do r--
c) Else swap str[l] and str[r]
Here's a solution that will do it "in place" in one pass.
bool isspecial(char c)
{
if ((c >= 'a') && (c <= 'z')) return false;
if ((c >= 'A') && (c <= 'Z')) return false;
return true;
}
void rev(char* array, int N)
{
int i = 0; // i points to the first index of the array
int j = N - 1; // j points to the last index of the array
while (i < j)
{
if (isspecial(array[i]))
{
i++;
}
else if (isspecial(array[j]))
{
j--;
}
else
{
char tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
}
}
Console.WriteLine("enter any string");
string str = Console.ReadLine();
string[] revstr = new string[str.Length];
for (int i = 0; i < str.Length; i++)
{
int ch = Convert.ToInt16(str.ToLower()[i]);
if ((ch < 97 || ch > 122))
{
revstr[i] = str[i].ToString();
}
}
for (int k = str.Length - 1; k >= 0; k--)
{
int ch = Convert.ToInt16(str.ToLower()[k]);
if (!(ch < 97 || ch > 122))
{
for (int j = 0; j < str.Length; j++)
{
if (revstr[j] == null)
{
revstr[j] = str[k].ToString();
break;
}
}
}
}
for (int s = 0; s < revstr.Length; s++)
{
Console.Write(revstr[s]);
}
If you want the position of the special characters to remain the same and the rest of the string to be reversed then this should work -
#include <iostream>
using namespace std;
void swap(char& a, char& b)
{
char temp = a;
a = b;
b = temp;
}
int main()
{
string s = "Hell$o World";
for(int i = 0, j = s.length() -1;i < s.length()/2; i++, j--) {
while((s[i] <= 'a' && s[i] >= 'Z') || s[i] >= 'z' || s[i] <= 'A') {
i++;
}
while((s[j] <= 'a' && s[j] >= 'Z') || s[j] >= 'z' || s[j] <= 'A') {
j--;
}
swap(s[i], s[j]);
}
cout << s << endl; //dlro$W olleH
return 0;
}

String error output [duplicate]

This question already has answers here:
C++ Remove punctuation from String
(12 answers)
Closed 9 years ago.
I got a code. It should give me an output that will erase the middle character between 'z' and 'p'. for example: zipZap("zipXzap"): expected [zpXzp] but found [z pXz p]
std::string zipZap(const std::string& str){
string a = str;
string b = "";
size_t len = str.length();
for (size_t i = 0; i < len; i++){
if (str[i] == 'z')
if (str[i+2] == 'p')
a[i+1] = ' ';
}
return a;
}
When i replaced a[i+1] = ''; it gave me an error.
You are not removing the chars, you are replacing them with ' '.
There are many ways to do this. One simple way is to build a new string, only adding chars when the proper conditions are met:
std::string zipZap(const std::string& str)
{
string a;
size_t len = str.length();
for (size_t i = 0; i < len; i++) {
// Always add first and last chars. As well as ones not between 'z' and 'p'
if (i == 0 || i == len-1 || (str[i-1] != 'z' && str[i+1] != 'p')) {
a += str[i];
}
}
return a;
}
Use string.erase() :
std::string zipZap(const std::string& str){
std::string a = str;
std::string b = "";
size_t len = str.length();
for (size_t i = 0; i < len; i++){
if (a[i] == 'z')
if (a[i+2] == 'p')
a.erase(i+1,1);
}
return a;
}
You're completely right that you cant replace an element of the string with ''.
A string is an array of chars, and '' is not a char at all. It is nothing.
If we look at the cplusplus page for a string
http://www.cplusplus.com/reference/string/string/
We see that we can use erase(iterator p) to "Erase characters from string (public member function)"
So if we change:
for (size_t i = 0; i < len; i++){
if (str[i] == 'z')
if (str[i+2] == 'p')
a.erase(a.begin() + i + 1);
We're closer now, but we can see that len is no longer the same as str.length(). the length of a is now actually 1 char shorter than len. To remedy this however we can simply add:
for (size_t i = 0; i < len; i++){
if (str[i] == 'z')
if (str[i+2] == 'p')
a.erase(a.begin() + i + 1);
len -= 1;
Hope that helps
If you #include <regex>, you can do a regular expression replacement.
std::string zipZap(const std::string& str){
regex exp("z.p");
string a = str;
a = regex_replace(a, exp "zp");
return a;
}