2

I am trying to write a code, which would do this:

Input: Sample
Output: Saammmppppllllleeeeee

This is what I've done so far:

#include <string>
#include <bits/stdc++.h>
#include <cstring>

using namespace std;

int main()
{
    string s;
 
    cout << "Input a word: ";
    cin >> s;
    
    int n = s.length();
 
    char word[n + 1];
 
    strcpy(word, s.c_str());
 
    for (int i = 0; i < n; i++)
    {
        if (i > i)
        {
            int x = i * i;
            cout << word[x];
        }
    }
 
    return 0;
}

This code is supposed to duplicate the next element of the array in this way:

  • word[0] is not duplicated
  • word[1] is duplicated once (two copies of the element)
  • word[2] is duplicated three times (three copies of the element) And so on.

How can I do this without using too many void functions?

Bernardo Duarte
  • 4,074
  • 4
  • 19
  • 34
pannadela
  • 23
  • 4
  • as a basic strategy, try reading the input and create a new string to which you append in a for loop the character you are reading. – Unlikus Apr 20 '21 at 18:18
  • 2
    Is there any reason you are using a char array like `char word[n + 1];` instead of another `std::string` like `std::string word;`? – Galik Apr 20 '21 at 18:22
  • 3
    Unrelated `#include ` suggests that you don't know what `#include ` does. Leave the [cargo cult](https://en.wikipedia.org/wiki/Cargo_cult_programming) behind and [learn what `#include ` does and why you shouldn't directly include it](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) – user4581301 Apr 20 '21 at 18:28

3 Answers3

4

If you look at the output, there is a pattern you can take advantage of. The 1st letter is printed 1 time, the 2nd letter is printed 2 times, the 3rd letter is printed 3 times, ...

Finding a pattern is a great step toward getting an algorithm.

I've made some changes to your code below and noted them. The biggest change was to the for loop. I simply take advantage of std::string constructors to add the correct amount of letters per character in the original string.

#include <string>
// #include <bits/stdc++.h>  // CHANGED: Just don't
// #include <cstring>  // CHANGED: Not needed
#include <iostream>  // CHANGED: Include what you use

// using namespace std;  // CHANGED: Bad practice

int main() {
  std::string s;

  std::cout << "Input a word: ";
  std::cin >> s;

  // CHANGED: Tweaked algorithm to adapt to observed pattern
  std::string newWord;
  for (std::size_t i = 0; i < s.length(); i++) {
    newWord += std::string(i + 1, s[i]);
  }
  std::cout << newWord << '\n';

  return 0;
}

Output:

Saammmppppllllleeeeee
sweenish
  • 4,793
  • 3
  • 12
  • 23
2

I would do it like this, one loop for the word and another inside for the amount to repeat for each letter:

int main()
{
    string s;
 
    cout << "Input a word: ";
    cin >> s;
    
    int n = s.length();
 
    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j <= i; ++j) {
            cout << s[i];
        }
    }
    cout << endl;
 
    return 0;
}
Bernardo Duarte
  • 4,074
  • 4
  • 19
  • 34
0

Thanks a lot for help! Meanwhile, I've tried to write something on my own and I wrote working one, which looks like this:

#include <iostream>
#include <string>
#include <bits/stdc++.h>
#include <cstring>

using namespace std;

int main()
{
    string s;
 
    cout << "Input a word: ";
    cin >> s;
    
    int n = s.length();
 
    char word[n + 1];
 
    strcpy(word, s.c_str());
 
    for (int i = 0; i < n; i++)
    {
        for (int k = 0; k < (i + 1); k++)
        {
            cout << word[i];
        }
    }
    return 0;
}

This was my first post here and I didn't even expect that someone would answer to this thread that quickly!

pannadela
  • 23
  • 4