[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

right filling string till length = 5

Posted on 2000-03-28
3
Medium Priority
?
236 Views
Last Modified: 2010-04-15
Here is my code for starters:
if(strlen(wc)==0)
{
    /*5 spaces */
    strcpy(wc,"     ");
    strcat(com_buffer, wc);
}
else
{  
   if(strlen(wc) != 5);
   {
       /* fill to the right with spaces till length = 5 */
       strcat(com_buffer, wc);
   }
   else strcat(com_buffer, wc);
}

Where the comment is to fill to the right is where I need to insert the code that will take the length of wc if less than 5 anf fill it to the right with spaces.  So if user inputs "a" wc becomes "a    " and so on.

TIA

Craig
0
Comment
Question by:csindorf
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 16

Accepted Solution

by:
imladris earned 200 total points
ID: 2664756
A simple rewrite for the whole thing could be:

while(strlen(wc)<5)strcat(wc," ");
strcat(com_buffer,wc);

0
 
LVL 16

Expert Comment

by:imladris
ID: 2664764
I'm assuming that since your first action in your code is to do a strlen on wc, wc does in fact contain a reasonable string, although it might be a null string.
Given that you can skip the first case since you can append stuff to null strings with strcat.
0
 

Author Comment

by:csindorf
ID: 2664887
BINGO
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question