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
Try this:

#include <stdio.h>

main(int argc, char *argv[])
{
       char str_in[20] = {0}, str_out[40] = {0};
       int i, j, len;

       printf("Enter your string => ");
       scanf("%s", str_in);

       len = strlen(str_in);

       i = 0; j = 0;
       while (str_in[i] != '\0')
       {
               str_out[j++] = str_in[i++];
               str_out[j++] = '*';
       }
       str_out[j - 1] = '\0';

       printf("Input String was => %s\n", str_in);
       printf("Output String is => %s\n", str_out);
}


-ssnkumar

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

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