You are here:

C/follow up to string manipulation

Advertisement


Question
Hi
Your previous answer was very helpful and it did solve the problem.  But I have trouble understanding the logic behind your code, especially the part when inputstr() and outstr() are used to put * between each letter of the string.  Could you explain it to me how that worked.  To freshen your mind, I typed in the code below.  
By the way, I would also like some advice on this problem:
Create a 10 x 10 times table, with a title row and colums.  Create 3 identical tables using nested for, while and do..while structures.  

Thank you again


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);  

Answer
Hi Katrina,

the variable "inputstr" contains the original string without the "*" character. We are creating a new string variable "outputstr" to store the manipulated value of the variable "inputstr".

Another way of doing this can be to just loop through and print the individual characters of "inputstr" as well as "*" alternately without using the second variable "outputstr". That would be more efficient in terms of memory.

About your second query , I'm not too sure what is your question.

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.