can\'t get this code to function properly can someone please help me. Below is t
ID: 3670225 • Letter: C
Question
can't get this code to function properly can someone please help me. Below is the assignment code and output. Thanks!
20.1 C++: Function with C string as argument
Write a function that takes as a parameter a C string and changes all the white spaces for dashes.
#include <iostream>
using namespace std;
void StrSpaceToHyphen(char modString[]);
int main() {
const int INPUT_STR_SIZE = 50; // Input C string size
char userStr[INPUT_STR_SIZE]; // Input C string from user
// Prompt user for input
cout << "Enter string with spaces: " << endl;
cin.getline(userStr, INPUT_STR_SIZE);
// Call function to modify user defined C string
StrSpaceToHyphen(userStr);
cout << "String with hyphens: " << userStr << endl;
return 0;
}
void StrSpaceToHyphen(char modString[]) {
int i=0;
int k=50;
for(i=0; i<=k; i++)
{
if (modString[i]==' ')
modString[i]='-';
if (modString[i]=='')
k=i;
};
}
Submitted: 08:42 am on 02/21/16
Total: 1/3
Unit test
0/2
Test various outputs
Compare output
1/1
Input
Hello there, what is happening?
Your output
Enter string with spaces: String with hyphens: Hello-there,-what-is-happening?
It will not pass the various output test
Input
Hello there, what is happening?
Your output
Enter string with spaces: String with hyphens: Hello-there,-what-is-happening?
It will not pass the various output test
Explanation / Answer
#include <iostream>
#include <cstring>
using namespace std;
// Function replaces spaces with hyphens
void StrSpaceToHyphen(char modString[]) {
int i = 0; // Loop index
for (i = 0; i < strlen(modString); ++i) {
if (modString[i] == ' ') {
modString[i] = '-';
}
}
return;
}
int main() {
const int INPUT_STR_SIZE = 50; // Input C string size
char userStr[INPUT_STR_SIZE]; // Input C string from user
// Prompt user for input
cout << "Enter string with spaces: " << endl;
cin.getline(userStr, INPUT_STR_SIZE);
// Call function to modify user defined C string
StrSpaceToHyphen(userStr);
cout << "String with hyphens: " << userStr << endl;
return 0;
}
output
Enter string with spaces:
hello world
String with hyphens: hello-world
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.