You are here:

C++/string manipulation

Advertisement


Question
Hi
For the following program, I understand how to use strlen(), but I couldn't print out a string with *.  
This is the requirement:
The function strlen() will tell you the number of characters in a string.  use a for loop to print out a string the user enters with a * between each letter.  Assume a maximum input string of 20 characters.  
input: "Charlie"
output: "C*h*a*r*l*i*e

Any suggestions?
Thank you

Answer
Hello Katrina,

This is not actually that difficult if you think about it. Something along the lines of:

// in main
int main()
{
char name[20] = {0};
unsigned int i;

cout << "Enter your name\n";
cin.getline(name);

for(i = 0; i < strlen(name); i++)
{
cout << name[i] << '*';
}
return 0;
}

As you can see, that should solve that problem, assuming we are talking C++ and not C, where the input and output methods would be different.

I hope this information was helpful.

- Eddie  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.