473,382 Members | 1,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Zero pad a string using strncpy and strncat

1
In the project I'm currently working on, one of the requirements is to use strncat and strncpy to zero pad a string so that it's length is equal to the word size that we set. For example if word size = 8, but the string only has 4 characters, then zero pad the string, so that its length is equal to 8.
So let's say we have 1011 but word size is 8, then we zero pad it to be 00001011.

This is my code so far for that part of the project:

int size = 8;

char *z = "0";
char *u;
u = malloc(size * sizeof(char) + 1);

int diff = size - (strlen(a)+1);


/*Zero Pad with strncpy and strncat*/
while (diff >= 0){
//size_t len = strlen(z) + strlen(u) + 1;
strncpy(u, z, strlen(a) + 10);
strncat(z, u, size);
printf("%s--check\n", u);
diff--;
}
size_t lenr1 = strlen(a) + strlen(u) + 1;
a = strncat(u, a, lenr1);
printf("%s %s %s\n", a, o, b);


It doesn't work. It compiles and everything, but it just repeats 0, 00, 0, 00. strncpy doesn't seem to copy the string after I strncat it. I can't figure out why, and I don't know what steps to take to figure it out.
Apr 25 '17 #1
1 5999
weaknessforcats
9,208 Expert Mod 8TB
Zero-fill right looks like:

Expand|Select|Wrap|Line Numbers
  1. int size = 8;    //size of string
  2.     char* z  = "0";     //one ASCII zero
  3.     char* str = (char*)malloc(size * sizeof(char) + 1);
  4.  
  5.     strcpy(str, "123");
  6.     while (strlen(str) < size)
  7.     {
  8.         strcat(str, z);
  9.  
  10.     }
strcat locates the \0 of the original string and replaces the \0 with its string.

In this example str is 123\0. strcat's string is 0\0.

After strcat, str is 1230\0. After another strcat, str is 12300\0. etc...

All you do is write a loop that repeats the strcat so long as there is room in str. In this example, str can have 7 characters plus 1 for the null.
Apr 26 '17 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Andrew Connell | last post by:
Having fits transforming an XML string using an XSL file. In the 1.1 version of the framework, I see that the XmlResolver is heavily used in the XslTransform class. However, that looks like I am...
12
by: babak | last post by:
Hi everyone I want to format a string (using sprintf) and put it in a messagebox but however I try to do it, it doesn't seem to work. Here is an example sample of what i try to do: char msg;...
2
by: Bruno | last post by:
Hello friends I'm creating one application that will send content to another app using HTTP connection. To authenticate my connection, I need to send a string with the username and password...
3
by: minguskhan | last post by:
Does anyone know how to reverse a string using a loop?
2
by: aurora | last post by:
I have some unicode string with some characters encode using python notation like '\n' for LF. I need to convert that to the actual LF character. There is a 'unicode_escape' codec that seems to...
11
by: wreckingcru | last post by:
I'm trying to tokenize a C++ string with the following code: #include <iostream> #include <stdio> #include <string> using namespace std; int main(int argc, char* argv)
4
by: lurch132002 | last post by:
i am trying to create an array of structs to hold some information but whenever i get to the second element and try to strncpy it i get a segmenation fault. ive searched around for similar...
8
by: Nkhosinathie | last post by:
i'm comparing two string using the function stryncmp,but my problem is when i execute the program it doesn't give me the correct values.actually it gives me same results as the function strycmp ...
2
by: powerfulperl | last post by:
I want to locate a string 'Local=IN' from a file and I am sure that this string is located within 100 lines(assumption) from the beginning of the file out of 5000 lines. The 100th line start with the...
5
by: sandeep n | last post by:
I am not getting reverse string using following code, can u explain where i did mistake and correct it with regards sandeep code-->>> #include<stdio.h>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.