- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Generate a sequence determined by the characters of a given string
In this article, we'll discuss an engaging problem related to strings and sequences. The problem statement is "Generate a sequence determined by the characters of a given string". This problem is an excellent way to enhance your skills in string manipulation and sequence generation.
Problem Statement
Given a string, the task is to generate a sequence where each character of the string is replaced by its position in the English alphabet.
Solution Approach
Our approach to this problem is straightforward. We will iterate over the string and for each character, we will calculate its position in the English alphabet. The position can be calculated as the ASCII value of the character minus the ASCII value of 'a' plus one.
Example
Here're the programs that solves the problem −
#include <stdio.h> #include <stdlib.h> #include <string.h> int* generateSequence(char* str) { int length = strlen(str); int* sequence = (int*)malloc(length * sizeof(int)); // Allocate memory for the sequence array for (int i = 0; i < length; i++) { int position = str[i] - 'a' + 1; // Calculate the position of the character in the alphabet sequence[i] = position; // Store the position in the sequence array } return sequence; } int main() { char str[] = "abc"; int* sequence = generateSequence(str); // Generate the sequence based on the input string printf("The generated sequence is: "); for (int i = 0; i < strlen(str); i++) { printf("%d ", sequence[i]); // Print the generated sequence } printf("\n"); free(sequence); // Free the dynamically allocated memory return 0; }
Output
The generated sequence is: 1 2 3
#include <iostream> #include <string> #include <vector> using namespace std; vector<int> generateSequence(string str) { vector<int> sequence; for (char c : str) { int position = c - 'a' + 1; sequence.push_back(position); } return sequence; } int main() { string str = "abc"; vector<int> sequence = generateSequence(str); cout << "The generated sequence is: "; for (int num : sequence) { cout << num << " "; } cout << endl; return 0; }
Output
The generated sequence is: 1 2 3
import java.util.ArrayList; import java.util.List; public class Main { public static List<Integer> generateSequence(String str) { List<Integer> sequence = new ArrayList<>(); for (char c : str.toCharArray()) { int position = c - 'a' + 1; // Calculate the position of the character in the alphabet sequence.add(position); // Add the position to the sequence list } return sequence; } public static void main(String[] args) { String str = "abc"; List<Integer> sequence = generateSequence(str); // Generate the sequence based on the input string System.out.print("The generated sequence is: "); for (int num : sequence) { System.out.print(num + " "); // Print the generated sequence } System.out.println(); } }
Output
The generated sequence is: 1 2 3
def generate_sequence(s): sequence = [] for c in s: position = ord(c) - ord('a') + 1 # Calculate the position of the character in the alphabet sequence.append(position) # Add the position to the sequence list return sequence if __name__ == "__main__": s = "abc" sequence = generate_sequence(s) # Generate the sequence based on the input string print("The generated sequence is:", *sequence) # Print the generated sequence
Output
The generated sequence is: 1 2 3
Explanation with a Test Case
Let's consider the string "abc".
When we pass this string to the generateSequence function, it replaces each character with its position in the English alphabet. 'a' is replaced with 1, 'b' is replaced with 2, and 'c' is replaced with 3.
So, the function returns the sequence 1, 2, 3.
Conclusion
This problem shows how we can manipulate strings and generate sequences based on the characters of a string. It's an excellent problem to practice your coding skills and to understand how to work with strings and sequences.