How to repeat the string in C++? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have to write a program in which integer value is entered from user and the string has to be displayed that many times. But I am getting errors.
#include<iostream>
#include<string>
using namespace std;
int main()
{
int N;
cout << "Enter N: ";
cin >> N;
cout << string(N, "Well Done");
return 0;
}
Note: I am not permitted to use a loop in this assignment.

If you may not use a loop, you may use goto to get around the restriction:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int N;
cout << "Enter N: ";
cin >> N;
{
int i = 0;
goto test;
begin:
cout << "Well Done";
++i;
test:
if (i < N)
goto begin;
}
return 0;
}
Note that goto is widely considered bad practice.

EDIT2: IN THE ORIGINAL ASKER's COMMENTS, LOOPS OF ANY KIND ARE PROHIBITED IN THIS ASSIGNMENT.
Use recursion.
void printN(int n, string s) {
if (n <= 0) return;
cout << s << endl;
printN(n-1, s);
}
Then you can call this from your main program as follows:
printN(userInput, "Hi my name is ricky bobby");
EDIT: just saw you haven't learned recursion yet. Look up this term, and familiarize yourself with it. This is a way to do iteration without looping (this is the most simplistic way I can describe it)

std::string does not have a constructor that repeats a string N times (it does have one for repeating a single character N times, though). What you need is a loop instead, eg:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int N;
cout << "Enter N: ";
cin >> N;
for (int i = 0; i < N; ++i)
cout << "Well Done";
return 0;
}

Related

can someone please help solving this issue [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
i appeared for a campus placement exam few days ago. then while solving it. I found it quite difficult to solve. And atlast i unable to solve it.
the question is as follows:
suppose we input a string of n length. then we have to give second input.that is number of charecters after which space needs to put.
example input:
joebiden
3
expected output
joe bid en
i code something like this.
#include <iostream>
using namespace std;
int main()
{
string str;
int space, con1 = 0, con2 = 0, con3 = 0, i = 0;
cin >> str;
cin >> space;
con1 = str.size();
for (con2 = 0; con2 < con1; con2++) {
for (con3 = 0; con3 < space; con3++) {
cout << str[con2];
if (con3 < (space - 1)) {
con2++;
}
}
cout << " ";
}
return 0;
}
You can use modulo operator to find on which index you should put space.
#include <iostream>
using namespace std;
int main() {
string str;
cin >> str;
int space;
cin >> space;
int n = str.length();
for (int i = 0; i < n; ++i) {
if (i % space == 0) cout << " ";
cout << str[i];
}
return 0;
}
This would be a working and also a shorter approach for your problem:
#include <iostream>
#include <string>
int main()
{
std::string input; std::cin >> input; // Ask the user for input
int segment_len; std::cin >> segment_len; // Ask the user for segment_len
if (segment_len > 0)
{
for (int i = segment_len; i < input.size(); i += segment_len)
{
input.insert(i++, 1, ' ');
}
}
std::cout << input;
return 0;
}
This application firstly takes in a string, then the segment length, and then adds a space after every segment_len characters.

How can I replace every alphabetical lower case letter in a string with its opposite alphabetical lower case letter in C++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I would like to find the opposite letter using for loop. Also, I would like to note that I am trying to find the opposite letter. For example, replacing "a" with "z", "b" with "y"...
For example, the user inputs this: "3 feg", and the output from this program will be: "uvt". Also, my constraint is 1<=n<=100. The input format is "n input_string_of_length_n", and the output format is "encrypted_string_of_length_n". As a new beginner to programming, I am lost and I do not know how to solve this. Any help will be very much appreciated.
This is my code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
`int` user_input_number;
string user_input_text;
cout << "Type: ";
cin >> user_input_number;
cout << "Type: ";
cin >> user_input_text;
for(char i = 'a'; i <= 'z'; i++)
{
cout << << endl;
}
return 0;
}
Here is one solution using ASCII arithmetic:
string s = "abc";
for(int i = 0; i < s.length(); i++){
s[i] = 219 - s[i];
}
cout << s; // "zyx"
The reason it works is that all ASCII characters are between 0 and 127, and this way the values loop back around
// Example program
#include <iostream>
#include <string>
#include <map>
using namespace std;
string encrypt(int n, string s) {
map<char, int> alphabetMap = { };
string alpha = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < 26; i++){
alphabetMap.insert({alpha[i],i});
}
string coded = "";
for (int i = 0; i < n; i++) {
int index = alphabetMap[s[i]];
coded += alpha[25 - index];
}
return coded;
}
int main()
{
int n = 0;
string s = "";
cout << "Enter n and string: " ;
cin >> n >> s;
cout << encrypt(n,s) << endl;
}

Sum of first and last digit of a number [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
The problem is to find sum of first and second digit of a number.
If number is smaller than 10 then print number itself.
I wrote this code but i don't know why output is wrong.
#include <iostream>
using namespace std;
int main()
{
int t,a,n,l;
cin>>t;
while(t--)
{
cin>>n;
if(n<10)
{
cout<<n;
}
else
{
a=n%10;
l=n/10;
cout<<a+l<<endl;
}
}
return 0;
}
The reason your solution is wrong because n/10 won't give you the first digit :)
If this is for a question for competitive programming(since you're taking in t test cases), I'll suggest taking input as a string assuming that input is always a valid int.
std::string s;
cin >> s;
if (s.size() == 1) std::cout << s << std::endl;
else std::cout << int(s.front() - '0') + int(s.back()-'0') << std::endl;
extracting of required digits is wrong in your solution.
Please find solution for adding first and last digit of a number at
https://onlinegdb.com/Byro9hCMI or below:
its small and hence pasting it here as well:
#include <iostream>
using namespace std;
int main()
{
int first, last, number;
cin>>number;
first = number % 10;
while((number = number/10)>0)
{
last = number;
}
cout << endl << first+last << endl;
return 0;
}
Two Add first two digit (unit and tens)at https://onlinegdb.com/SJXNThAzI or below:
#include <iostream>
using namespace std;
int main()
{
int first, second, number;
cin>>number;
first = number % 10;
number = number/10;
second = number %10;
cout << endl << first+second << endl;
return 0;
}

Convert lowercase to uppercase in a string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
The new typist in the printing cell is typing carelessly the jobs assigned. The typist while was supposed to type all the characters in upper case, has got in lower cases too. Your duty is to verify if all the characters are in upper case and do so if not. Also, notify how many mistakes the typist did.
Input bEGIN
Output BEGIN 1
I am getting wrong answer in some of the cases please help i am beginner
n=length of string
1<=n<=50
int main() {
string s;
cin >> s;
int ans = 0;
for (auto &c : s) {
if (islower(c)) {
ans++;
c = toupper(c);
}
}
cout << s;
cout << endl;
cout << ans;
return 0;
}
I think it includes spaces too, so instead of using >> operator use getline(cin,string), as >> gets terminate when whitespace is occurred.
#include <iostream>
using namespace std;
int main() {
string s;
getline(cin,s);
int ans = 0;
for (auto &c : s) {
if (islower(c)) {
ans++;
c = toupper(c);
}
}
cout << s;
cout << endl;
cout << ans;
return 0;
}
This might be a solution to other test cases.
This should work:
#include<stdio.h>
#include<iostream>
#include<string>
using namespace std;
int main() {
string s;
getline(cin,s);
int ans = 0;
for (auto &c : s) {
if (islower(c)) {
ans++;
c = toupper(c);
}
}
cout << s;
cout << endl;
cout << ans;
return 0;
}

I am getting C++ syntax error [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
This script is supposed to read chars from keyboard, store them into arrays, and then output them:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void storeArraysintoStruct(char[], int);
int main()
{
char test[] ="";
int a = 0;
storeArraysintoStruct(test, a);
system("pause");
return 0;
}
void storeArraysintoStruct(char test[], int a)
{
int n;
cout << "Enter number of entries: " << endl;
cin >> n;
int i = 0;
for (i=0, i<n, i++)
{
cout << "Enter your character: " << endl;
cin.getline(test, n);
}
while (i < n)
{
cout << test[i] << endl;
i++;
}
}
Edit: fixed it:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void storeArraysintoStruct(char[], int);
int main()
{
char test[40] = "";
int a = 0;
storeArraysintoStruct(test, a);
system("pause");
return 0;
}
void storeArraysintoStruct(char test[], int a)
{
int n;
cout << "Enter number of entries: " << endl;
cin >> n;
int i;
for (i=0; i < n; i++)
{
cout << "Enter your character: " << endl;
cin >> test[i];
if (test[n-1])
{
cout << endl;
}
}
i =0;
while (i < n)
{
cout << test[i] << endl;
i++;
if(test[n-1])
{
cout << endl;
}
}
}
However, I am getting the errors Expected: primary expression before ")" and ";" before while. Any help will be greatly appreciated.
Edit: The script doesn't work as expected, for it doesn't output the stored characters. Any advice would be greatly appreciated.
The syntax error has already been pointed out in the comments. Also, as it has been mentioned, you never reset i after for loop, which prevents your while loop from running.
However, you have to also take in mind that this
char test[] = "";
allocates array test of only 1 character long. You cannot put more than one character of data into that array. In other words, your storeArraysintoStruct is sure to overrun the array and fall into undefined behavior territory.
In you want to preallocate a larger buffer for future use in storeArraysintoStruct, you have to specify the size explicitly. For example
char test[1000] = "";
will make test an array of 1000 characters. Of course, regardless of how large the array is, it is your responsibility to observe the size limit.
P.S. What is the point of that parameter a, if you never use it inside storeArraysintoStruct?