Loop Through String
Loop through each character
Print index number and Character on a line
INPUT
001
OUTPUT:
Index 0: 0 Index 1: 0 Index 2: 1
#include <iostream>
#include <string>
using namespace std;
The code I have so far:
int main(){
string h;
string i;
cin >> i;
for (i=0; i<h; i++){
if (h%i==0){
cout << i << endl;
}
}
return 0;
}
Please help. Thank you.
#include <iostream>
#include <string>
using namespace std;
int main(){
string h;
int i;
cin >> h;
for (i=0; i<h.length(); i++){
cout << "Index " << i << ": "<< h[i]<<endl;
}
return 0;
}


Loop Through String Loop through each character Print index number and Character on a line INPUT...
15.6: Fix the code to print the count from 1 to x (to loop x times) #include <iostream> // include the header file using namespace std; void count(int x){ for(int i=1; i<=x; i++){ cout<<x; } } int main(){ int x,i; cin >> x; count(x); return 0; } 15.7 Fix the nested for loops to print the count from 1 to x twice, e.g. "12....x12.....x" #include <iostream> // include the header file using namespace std; int main(){...
3.14.1: String with digit. Set hasDigit to true if the 3-character passCode contains a digit. HELP ME FIX THIS #include <iostream> #include <string> #include <cctype> using namespace std; int main() { bool hasDigit; string passCode; hasDigit = false; cin >> passCode; for(int i=0;i<passCode.length();i++) { if(isdigit(passCode[i])) { hasDigit=true; break; } } if (hasDigit) { cout << "Has a digit." << endl; } else { cout << "Has no digit." << endl; } return 0; }
I need to add a for or a while loop to the following code. please help needs to be in c++. #include <iostream> #include <string.h> using namespace std; int main() { char input[100]; cout << "Please enter a character string: "; cin >> input; char *head = &input[0], *tail = &input[strlen(input) - 1]; char temp = *head; *head = *tail; *tail = temp; tail--; head++; cout << "CString = " << input << "\n"; }
Fix the following code to print whether the input is positive or negative #include <iostream> #include <string> using namespace std; void positiveOrNegative(int); // declare function int main(){ int y; int result; cin >> y; result = positiveOrNegative(y); return 0; } void positiveOrNegative(int x){ if (x>=0) cout << "Positive"; else cout << "Negative"; }
Write a program that takes in a line of text as input, and outputs that line of text in reverse. The program repeats, ending when the user enters "Quit", "quit", or "q" for the line of text.Ex: If the input is:Hello there Hey quitthen the output is:ereht olleH yeHThere is a mistake in my code I can not find. my output is : ereht olleH ereht olleHyeHHow can I fix this?I saw some people use void or the function reverse but we didnt...
Explain each line of the following code (Briefly) and give an overall reflection on using these particular functions. #include<iostream> #include<string> using namespace std; //declare a struct with field mentioned struct Unit { string unit_code; int unit_credict_hrs; int unit_semister_study; string prerequisites; string post_requisites; string number_post_requisites; }; int main() { Unit unit1; string str; cout << "Enter a tring: "; getline(cin, str); size_t index = 0; string tok,t, del=","; ...
My C++ code won't loop like it should. What am I doing wrong? #include <iostream> using namespace std; int main() { int n; int flag= 1; cout << "Prime/Not Prime" << endl; cout << "Enter the Number to check Prime:" << endl; cin >> n; int m = n / 2; int i; for (i = 2; i <= m; i++) if (n % i == 0) ...
Could use some help with this, Instructions: You will need to add an input statement for the grade variable to this code. Also add a for loop so that you can enter more than one grade. Code the for loop so that at least 7 grades can be entered. #include <iostream> #include <string> using namespace std; void main() { char grade; for (int x = 0; x < 7; x++) { cout << "Enter a...
Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with userNum initially 9 and user input of 5, 2, -1, then with userNum initially 0 and user input of -17, then with userNum initially -1. See "How to Use zyBooks". . Also note: If the submitted code has an infinite loop, the system will...
4) What is the output if the input istom - Sawyer? #include <iostream> using namespace std; int main() { string playerName; cout << "Enter name"; cin >> playerName; cout << endl « playerName; return 0; } a. Tom - Sawyer b. Tom Sawyer c. Tom d. Sawyer 5) Which XXX generates "Adam is 30 years old." as the output? #include <iostream> using namespace std; int main() { string name = "Adam"; int age = 30; XXX return 0; } a....