Table of Contents
Recursive algorithm generates subsequent strings
Syntax for subsequent arrays
Following method
Convert string using C
Example 5
Output
Use C to perform unary operations on strings
Use C to perform binary operations on strings
Use C to print all subsequent strings
Convert a string to have abcd...z as a subsequence
示例5
输出
结论
Home Backend Development C++ Convert a string to a form that has abcd..z as a subsequence

Convert a string to a form that has abcd..z as a subsequence

Sep 01, 2023 pm 02:17 PM
String conversion subsequence form Letter sequence conversion

Convert a string to a form that has abcd..z as a subsequence

String conversion (also known as string conversion) is an operation in C that stores the result in an output array after the entire process is executed. In C, there is a function called "transform()", which exists in the directory of the C environment, through which we can convert a string into a new string.

There are two forms of conversion functions −

  • Unary operation

    • The operation is applied to each element of the input array.

    • After the operation is completed, the results will be stored in an output array.

  • Binary operations

  • The operation applies to each element of a specific array.

  • The first input element and the second corresponding input element participate in the operation.

  • The output data will be stored in an output array.

A subsequence string is a brand new string generated by performing various operations on the input string (for example: deletion). For subsequence strings, the operation occurs without affecting the remaining characters.

For string conversion, the input contains an operation string of length n 1. The original characters belong to the series a to z. The length of the print string is treated as n here, which is an output string.

In this article, we will learn how to convert a string in C environment to have abcd….z as a subsequence.

Recursive algorithm generates subsequent strings

By using a recursive approach, the following is a possible algorithm for a subsequent string. This is a specific string and T is the time it takes to complete the operation.

  • Step 1 - Count the number of occurrences.

  • Step 2 - If i = length(s) and j = length(T).

  • The third step − then returns 1.

  • Step 4 - End.

  • Step 5 - If i = length(S).

  • Step 6 - Then return 0.

  • Step 7 - End.

  • Step 8 − Count

  • Step 9 - If, j

  • Step 10 − Count

  • Step 11 - End.

  • Step 12 - Count

  • Step 13 - Return the count.

  • Step 14 - End.

Syntax for subsequent arrays

Here, we have two given sequences. X and Y.
Initialize a table with a dimension of X.length * Y.length
X.label1 = X
Y.label2 = Y
CS1[0][] = 0
CS2[][0] = 0
Start from CS[1][1]
Compare X[i] and Y[j]
   If
      X[i] = Y[j]
      CS[i][j] = 1 + CS[i-1, j-1]
      Point an arrow to CS[i][j]
   Else
      CS[i][j] = max(CS[i-1][j], CS[i][j-1])
      Point an arrow to max(CS[i-1][j], CS[i][j-1])
Copy after login

Here we create a basic working syntax for subsequent arrays. When there are two sequences, we have to follow the following steps to get the output.

Following method

  • Method 1−Use C to convert string

  • Method 2 for unary operations on strings using C

  • Method 3 of using C to perform binary operations on strings

  • Use C to print all possible subsequent strings

  • Method to convert string to having abcd….z as subsequence using C 5

Convert string using C

In this C code, we create a new string and remove all vowels from the input string. # is added in place of these vowels.

Example 1

#include <bits/stdc++.h>
using namespace std;
string change_case(string r) {
   int l = r.length();
   for(int i = 0 ; i < l ; i++) {
      if(r[i] >= 'a' && r[i] <= 'z')
      r[i] = r[i] - 32;
      else if(r[i] >= 'A' && r[i] <= 'Z')
      r[i] = r[i] + 32;
   }
   return r;
}
string delete_vowels(string a) {
   string temp = "";
   int l = a.length();
   for(int i = 0 ; i < l ; i++) {
      if(a[i] != 'a' && a[i] != 'e' &&
      a[i] != 'i' && a[i] != 'o' &&
      a[i] != 'u' && a[i] != 'A' &&
      a[i] != 'E' && a[i] != 'O' &&
      a[i] != 'U'&& a[i] != 'I')
      temp += a[i];
   }
   return temp;
}
string insert_hash(string a) {
   string temp = "";
   int l = a.length();
   for(int i = 0 ; i < l ; i++) {
      if((a[i] >= 'a' && a[i] <= 'z') ||
      (a[i] >= 'A' && a[i] <= 'Z'))
      temp = temp + '#' + a[i];
      else
      temp = temp + a[i];
   }
   return temp;
}
void transformSting(string a) {
   string b = delete_vowels(a);
   string c = change_case(b);
   string d = insert_hash(c);
   if(d=="")
   cout<<"-1"<<endl;
   else
   cout << d<<endl;
}
int main() {
   string a = "RudraDevDas!!";
   string b = "aeiou";
   transformSting(a);
   transformSting(b);
   return 0;
}
Copy after login

Output

#r#D#R#d#V#d#S!!
-1
Copy after login

Use C to perform unary operations on strings

In this particular code, we show how to perform unary operations on the input array. This function accepts a pointer to the start and end position of a single input. And operate at the beginning of the output array.

The Chinese translation of

Example 2

is:

Example 2

#include <iostream>
#include <algorithm>
using namespace std;
int op_increment (int x) {
   x = x + 1;
   return x;
}
int main () {
   int n = 5;
   int input_array[] = {7, 16, 10, 97, 2001};
   int output_array[n];
   std::cout << "Input array present here:";
   for(int i=0; i<5; i++){
      cout << ' ' << input_array[i];
   }
   cout << '\n';
   transform (input_array, input_array+5, output_array, op_increment);
   std::cout << "The output array now contains with:";
   for(int i=0; i<5; i++){
      cout << ' ' << output_array[i];
   }
   cout << '\n';
   return 0;
}
Copy after login

Output

Input array present here: 7 16 10 97 2001
The output array now contains with: 8 17 11 98 2002
Copy after login

Use C to perform binary operations on strings

In this particular code, we show how to perform binary operations on the input array. The function transform() adds a pointer between the starting point and the first input array. Remember that binary operations always operate on two input data sets.

The Chinese translation of

Example 3

is:

Example 3

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int op_add (int i, int j) {
   return i+j;
}
int main () {
   int n = 5;
   int arr1[] = {7, 16, 10, 2001, 1997};
   int arr2[] = {1, 2, 3, 4, 5};
   int output[n];
   std::cout << "Input data in array1:";
   for(int i=0; i<n; i++){
      cout << ' ' << arr1[i];
   }
   cout << '\n';
   std::cout << "Input data in array2:";
   for(int i=0; i<n; i++){
      cout << ' ' << arr2[i];
   }
   cout << '\n';
   std::transform (arr1, arr1+n, arr2, output, op_add);
   std::cout << "Output array is here now:";
   for(int i=0; i<5; i++){
      cout << ' ' << output[i];
   }
   cout << '\n';
   return 0;
}
Copy after login

Output

Input data in array1: 7 16 10 2001 1997
Input data in array2: 1 2 3 4 5
Output array is here now: 8 18 13 2005 2002
Copy after login

Use C to print all subsequent strings

Apply the concepts of selection and non-selection to find all subsequences of a specific array. During this process, some characters may be removed without changing the order of the elements. Here, the time complexity of this process is O(2^n) and the space complexity is O(n).

Example 4

#include <bits/stdc++.h>
using namespace std;
void printSubsequence(string input, string output) {
   if (input.empty()) {
      cout << output << endl;
      return;
   }
   printSubsequence(input.substr(1), output + input[0]);
   printSubsequence(input.substr(1), output);
}
int main() {
   string output = "";
   string input = "rudraabonikoaa";
   printSubsequence(input, output);
   return 0;
}
Copy after login

Output

rudraabonikoaa
rudraabonikoa
rudraabonikoa
rudraaboniko
rudraabonikaa
rudraabonika
rudraabonika
rudraabonik
rudraabonioaa
rudraabonioa
rudraabonioa
rudraabonio
rudraaboniaa
rudraabonia
rudraabonia
Copy after login

Convert a string to have abcd...z as a subsequence

This is a specific procedure for converting a string into a form that has abcd...z as a subsequence.

  • Initialization characters.

  • If the length is less than 26, return false.

  • Iterate the loop from 0 to s.size() - 1.

  • If the character reaches z, break out of the loop.

  • If the current character is less than s or equal to character.

  • Replace the increment of the current character with 1.

  • If the character is less than or equal to z, return false.

  • Otherwise, return true.

在这个过程中,时间复杂度为O(n),辅助空间为O(1)。这里,n是特定字符串的长度。

Example 5

的中文翻译为:

示例5

#include <bits/stdc++.h>
using namespace std;
bool transformString(string& s) {
   char ch = 'a';
   if (s.size() < 26)
   return false;
   for (int i = 0; i < s.size(); i++) {
      if (int(ch) > int('z'))
      break;
      if (s[i] <= ch) {
         s[i] = ch;
         ch = char(int(ch) + 1);
      }
   }
   if (ch <= 'z')
   return false;
   return true;
}
int main() {
   string str = "aaaaaaaaaaaaaaaaaaaaaaaaaaa";
   if (transformString(str))
   cout << str << endl;
   else
   cout << "Not Possible" << endl;
   return 0;
}
Copy after login

输出

abcdefghijklmnopqrstuvwxyza
Copy after login

结论

在本文中,我们学习了使用C++环境进行字符串转换及其不同形式。通过遵循特定的算法和语法,我们检查和构建了一些不同的C++代码,并了解了如何转换字符串,使其具有abcd...z作为子序列。

The above is the detailed content of Convert a string to a form that has abcd..z as a subsequence. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Method to convert a string into a Boolean value using the parseBoolean() method of the Boolean class Method to convert a string into a Boolean value using the parseBoolean() method of the Boolean class Jul 26, 2023 pm 08:17 PM

How to convert a string into a Boolean value using the parseBoolean() method of the Boolean class. In Java programming, you often encounter situations where you need to convert a string into a Boolean value. The Boolean class in Java provides a very convenient method - parseBoolean(), which can convert strings into corresponding Boolean values. This article will introduce the use of this method in detail and provide corresponding code examples. First, we need to understand the parseBoolean() method

Quickly learn to convert string to array in Go language Quickly learn to convert string to array in Go language Mar 12, 2024 pm 10:27 PM

Quickly learn to convert strings to arrays in Go language. Conversion between strings and arrays is a common operation in Go language. Especially when processing data, you often encounter the need to convert strings into arrays. Condition. This article will introduce how to quickly learn to convert strings to arrays in Go language, so that you can easily deal with similar problems. In Go language, we can use the Split function provided by the strings package to split a string into an array according to the specified delimiter. The following is a

Convert string to double precision floating point number using java's Double.parseDouble() function Convert string to double precision floating point number using java's Double.parseDouble() function Jul 26, 2023 am 09:25 AM

Convert a string to a double-precision floating point number using Java's Double.parseDouble() function In Java programming, we often need to convert a string to a numeric type. For double-precision floating-point numbers, Java provides a very convenient method, the Double.parseDouble() function. This article will introduce the usage of this function and attach some sample code to help readers better understand and use this function. Double.parseDouble() function is

Convert string to StringBuilder in Java Convert string to StringBuilder in Java Sep 02, 2023 pm 03:57 PM

The append() method of StringBuilder class accepts a String value and adds it to the current object. Convert string value to StringBuilder object - Get string value. Append using the append() method to get the string into the StringBuilder. Example In the following Java program, we are converting an array of strings into a single StringBuilder object. Real-time demonstration publicclassStringToStringBuilder{ publicstaticvoidmain(Stringargs[]){&a

Convert string to hexadecimal and achieve reverse output using PHP Convert string to hexadecimal and achieve reverse output using PHP Mar 21, 2024 pm 03:33 PM

Title: Use PHP to convert strings to hexadecimal and achieve reverse output. In daily development, we sometimes need to convert strings to hexadecimal representation for data transmission or encryption. This article will introduce how to use PHP to convert a string into hexadecimal and realize the reverse output function. First, we need to write a PHP function to convert a string to hexadecimal. The following is an example code: functionstringToHex($string)

Convert string to lowercase using PHP function 'strtolower' Convert string to lowercase using PHP function 'strtolower' Jul 24, 2023 pm 12:34 PM

Convert a string to lowercase using PHP function "strtolower" In PHP, there are many functions that can be used to convert the case of a string. One of the very commonly used functions is strtolower(). This function converts all characters in a string to lowercase. Here is a simple example code showing how to use the strtolower() function to convert a string to lowercase: &lt;?php//original string $string="

How to convert a string to uppercase using Python's upper() function How to convert a string to uppercase using Python's upper() function Nov 18, 2023 pm 01:14 PM

How to convert a string to uppercase using Python's upper() function, specific code example required Python is a simple and easy-to-learn programming language that provides many built-in functions to handle strings. One of the commonly used functions is the upper() function, which converts all letters in a string to uppercase. This article will introduce in detail how to use Python's upper() function and provide corresponding code examples. First, let us understand the usage of upper() function. up

How to use the LOWER function in MySQL to convert a string to lowercase How to use the LOWER function in MySQL to convert a string to lowercase Jul 12, 2023 pm 09:09 PM

How to use the LOWER function in MySQL to convert a string to lowercase. In the MySQL database, we often encounter situations where we need to convert a string to lowercase, such as converting the user name entered by the user to lowercase for verification, or performing verification on a certain column. Case-insensitive search. At this time, you can use the MySQL built-in function LOWER to complete this task. The LOWER function is a string function that converts uppercase letters in a string to lowercase. Use the LOWER function to easily convert words

See all articles