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
Hi Katrina,

should be something like this -


char inputstr[30], outputstr[60];
int len=0,i=0,j=0,k=0;

printf("Enter a string: \n");
scanf("%s",&inputstr);
len = strlen(inputstr);
j=0;
k=0;
for(i=0;i<2*len-1;i++)
{
  if(i%2==0)
    {
    outputstr[j++]=inputstr[k++];
    }
  else
     {
     outputstr[j++] = '*';
     }
}
outputstr[j++]='\0';
printf("The output string is %s",outputstr);


HTH.

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Kaustav Neogy

Expertise

i can answer queries related to general programming constructs in C.

Experience

i have been programming in c since 1998.

©2012 About.com, a part of The New York Times Company. All rights reserved.