Suppose you are given a string str and two positions i and j, where i comes before j. The following pseudocode describes how to swap two letters in a word.
We are given a string str and two positions i and j. (i comes before j)
Set first to the substring from the start of the string to the last position before i.
Set middle to the substring from positions i + 1 to j - 1.
Set last to the substring from position j + 1 to the end of the string.
Concatenate the following five strings: first, the string containing just the character at position j, middle, the string containing just the character at position i, and last.
Check this pseudocode, using the string "Gateway" and positions 2 and 4. Draw a diagram of the string that is being computed, similar to Figure 5.
Want to see the full answer?
Check out a sample textbook solutionChapter 2 Solutions
Big Java Late Objects
Additional Engineering Textbook Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
Starting Out with Python (3rd Edition)
Modern Database Management
Starting Out with Java: From Control Structures through Objects (6th Edition)
Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)
Starting Out With Visual Basic (7th Edition)
- in pythonarrow_forwardIn C Languagearrow_forwardQ1__;. A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. Given a string s, return true if it is a palindrome, or false otherwise. Example 1: Input: s = "A man, a plan, a canal: Panama" Output: true Explanation: "amanaplanacanalpanama" is a palindrome. Example 2: Input: s = "race a car" Output: false Explanation: "raceacar" is not a palindrome..arrow_forward
- Q. Hi andre Write a Java program to check if a given string is a palindrome or not. A palindrome is a word, phrase, number, or other sequence of characters that reads the same backward as forward, ignoring spaces, punctuation, and capitalization. For example, "racecar" and "Madam" are palindromes, while "hello" and "Java" are not. Your program should take a string as input and return true if it's a palindrome, and false otherwise. Ok.arrow_forwardThe following C program requests four string from the user, sorts the four strings alphabetically and prints out the strings alphabetically as well as the string length. A typical usage input and output is shown below: Enter a string less than 50 characters in length: zebraEnter a string less than 50 characters in length: monkeyEnter a string less than 50 characters in length: baboonEnter a string less than 50 characters in length: apeape, [3]baboon, [6]monkey, [6]zebra, [5] Complete the following C program using the code provided. #include <stdio.h>#include <string.h> void swapstr(char *str1, char *str2); void main(void){ //line 1 int i = 0, j = 3; do { printf("Enter a string less than 50 characters in length: "); //line 2 i++; //line 3 //sort strings alphabetically //line 4 //line 5 //line 6 }//for j--; }//while //print sorted strings and its length //line 7 //line 8 }}//main void…arrow_forwardWrite a code that determines if a string is a palindrome (can be read forward and backward with the same meaning); e.g. "civic", “elle", “madam" 66 22arrow_forward
- In pythonarrow_forwardLanguage: Java Write a program that takes a String as input from the keyboard, representing a year. Your program should do the following: If the year entered has two characters, convert it to an int, add 2000 to it, and output it. If the year entered has four characters, just convert it to an int and output it. If the year entered has neither two nor four characters, output that the year is not valid.arrow_forward3. Capital of the Country by CodeChum Admin I'm a passionate tourist, touring to a lot of countries in the world. I want to go to the capitals of each of the countries since they say they're usually the best. Can you help me find where the capital is? Instructions: Ask the user for a string input From the string inputted, find the index of the capital letter. For this problem, it is guaranteed that there is only 1 capital letter in the inputted string. Hint: Use the isupper(char) function from the ctype.h library to check if a letter is capital or not. Input 1. Input string Output Enter·string:·Philippines 0arrow_forward
- 3. Capital of the Country by CodeChum Admin I'm a passionate tourist, touring to a lot of countries in the world. I want to go to the capitals of each of the countries since they say they're usually the best. Can you help me find where the capital is? Instructions: Ask the user for a string input From the string inputted, find the index of the capital letter. For this problem, it is guaranteed that there is only 1 capital letter in the inputted string. Hint: Use the isupper(char) function from the ctype.h library to check if a letter is capital or not. Input 1. Input string Output Enter string: Philippines 0arrow_forwardGiven a string on one line and an integer number on a second line, add a copy of the last number of characters of the string to the string's end. Then, output the result. Ex: If the input is: Fuzzy bear 4 the output is: Fuzzy bearbear Note: Using a pre-defined string function, the solution can be just one line of code. #include <iostream>#include <string>using namespace std; int main() { string strVar; int substrLen; getline(cin, strVar); cin >> substrLen; /* Your code goes here */ cout << strVar << endl; return 0;}arrow_forwardTake input a string s from the user and print the number of characters less than 's' in the string. String may contain any character. Programming Language- Javaarrow_forward
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning