chore(CPlusPlus): delete alternate characters in a string (#480)
Co-authored-by: Arsenic <54987647+Arsenic-ATG@users.noreply.github.com>pull/485/head
parent
928baa858f
commit
464b32a3d6
|
@ -96,7 +96,9 @@
|
|||
3. [String reversal](Strings/string-reverse.cpp)
|
||||
4. [String tokanisation](Strings/string-tokeniser.cpp)
|
||||
5. [Anagram check](Strings/anagram.cpp)
|
||||
6. [Remove occurrences from string](algorithms/CPlusPlus/Strings/remove-occurrences.cpp)
|
||||
6. [Remove occurrences from string](Strings/remove-occurrences.cpp)
|
||||
7. [Delete alternate characters in a string](Strings/delete-alternate-characters.cpp)
|
||||
8. [Print first letter of every word](Strings/print-first-letter.cpp)
|
||||
|
||||
## Trees
|
||||
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
/*
|
||||
Description: To delete alternate characters in a string
|
||||
Approach: To iterate over string and append the alternate characters in the output string
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
//function
|
||||
string delAlternate(string S) {
|
||||
// declaring an output string
|
||||
string r;
|
||||
//iterating over string and incrementing i by 2 to take alternate characters
|
||||
for(int i=0;i<S.length();i+=2) {
|
||||
r+=S.at(i);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
//main starts
|
||||
int main() {
|
||||
std::cout << "Enter a string: \n";
|
||||
string s;
|
||||
cin>>s;
|
||||
cout<<delAlternate(s);
|
||||
}
|
||||
|
||||
/*
|
||||
Sample Input:
|
||||
Enter a string:
|
||||
hello
|
||||
output:
|
||||
hlo
|
||||
*/
|
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
Description: A C++ program to print first letter of every word in a string
|
||||
*/
|
||||
|
||||
//importing c++ libraries
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
using namespace std;
|
||||
|
||||
//function
|
||||
string firstAlphabet(string s){
|
||||
//declaring an output string 'o'
|
||||
string o;
|
||||
|
||||
//inserting 1st letter in the output string if it's not starting with space
|
||||
if (s[0] != ' ') {
|
||||
o += s[0];
|
||||
}
|
||||
//iterating over the string from index 1
|
||||
//if space is found, then the next character is appended in the output string
|
||||
for (int i = 0; i < s.size(); i++) {
|
||||
if (s[i] == ' ' && s[i + 1] != ' ') {
|
||||
o += s[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
//returning the string
|
||||
return o;
|
||||
}
|
||||
|
||||
//main starts
|
||||
int main() {
|
||||
std::cout << "Enter a string: \n";
|
||||
string s;
|
||||
getline(cin, s);
|
||||
cout << firstAlphabet(s);
|
||||
}
|
||||
|
||||
/*
|
||||
Sample Input:
|
||||
Enter a string:
|
||||
Hello everyone, it's Jamie!
|
||||
|
||||
Output:
|
||||
HeiJ
|
||||
*/
|
Loading…
Reference in New Issue