It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I want reverse the string in particular format.
For example, "My name is Nishant" should be converted to "Nishant is name My".
If you have your words in a char[] words array then it is a simple loop:
for (i = 0; i < mid; i++)
exchange(words[i], words[number_of_words - i]);
for sane definitions of mid, number_of_words and exchange.
If all you have is a big char containing the entire statement, doing a strtok first is helpful. Then, use the above loop.
Give our regards to your instructor. If this is a homework assignment, you should write the code yourself.
Here is a little hint, though: use a char pointer to iterate through each character in the array until you hit the NUL terminator at the end. Now iterate in reverse until you hit a space. Save your place in another pointer, move forward by one then copy each of the characters up to but not including the NUL into your output buffer.
Now retrieve the position of that last space in that other pointer where you saved your place, and back up again. When you move forward, you actually need to stop when you encounter either a space of a NULL - ASCII '\0' or a zero byte - and not just a NUL.
It would be a little faster if you save the positions of each of the spaces in some kind of list as you iterate forward at the beginning. That way you don't then need to iterate backward over the entire string, with short iterations over each word. The code would be a little more complicated.
The increased efficiency would be insignificant for short strings like individual English sentences, but would be quite a lot of you were reversing a large file that you just read into memory.
Related
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
For a homework assignment I created a simple compression/decompression program that makes use of a naive implementation of run-length encoding. I've gotten my program working; compressing and decompressing any text file with a pretty large number of characters (e.g. the program source) works flawlessly. As an experiment I tried to compress/decompress the binary of the compression program itself. This resulted in a file that was much smaller than the original binary, and is obviously un-runnable. What is causing this data-loss?
My assumption was that it's related to how binary files are represented, but I can't figure much out past that.
Possible issues:
Your program opens the binary file in the text mode, which damages the '\r' and '\n' bytes
Your program incorrectly handles zero bytes, treating them as ends of strings ('\0') and not as data of its own
Your program uses char (that is actually signed char) for the bytes of data and correctly works only with non-negative values, which ASCII chars of English text are, but fails to work with arbitrary char/byte values, which may be negative
Your program has an overflow somewhere which shows up only on big files
Your program has some other data-dependent bug
If the platform is linux (as the question is tagged), there's no difference between binary and text modes. So it shouldn't be that; but even so, the files should be opened as binary.
I suspect that your problem is the program treats '\0' characters as terminators (or otherwise specially) instead of as valid data.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Is it a good idea to process two-dimensional arrays using pointers?
for( p = &a[0][0]; p < &a[N][N]; p++){
*p = 0;
}
Or is it better to use indexing?
The way you have denoted your array doesn't guarantee that pointer arithmetic will always work. Given how you've declared your two-dimensional array, it is probably not an issue, but it is certainly not good style.
However, imagine instead the array is allocated dynamically. Something like this:
int **p = malloc(ARR_SIZE * sizeof(*p));
for (i = 0; i < ARR_SIZE; i++) {
p[i] = malloc(ARR_SIZE * sizeof(**p));
}
This is a two-dimensional array (of sorts), but will not guarantee that allocated memory will be contiguous and hence would break your loop.
You should alway use indexing, unless you are absolutely sure that you know what you are doing, and have some very good reason why you don't want to use indexing for array
It is much easier to understand and maintain. If someone else is looking at your code it will be easier for them, also it is more resistant to errors. Pointers are great thing, but lot of people use them in wrong way and abuse them, making programs difficult to understand, maintain and find and solve bugs in them. From my professional experience it is much better not to create "magic" with pointers unless you really have to and are absolutely sure that you know what you are doing.
No, it is not as data may not be stored cosecutively (the above code assumes that the first element of a row start exactly after the last element of the previous row).
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
For example, I have a file, which says
char,5
int,6
Reading the above file, is it possible to declare 2 variable array in the code? So in future
if i add a new line it will automatically declare?
No, not in C.
You will need to write a script which reads this file and writes the c program.
In short, what you need is a C Source Code Generator.
Sure, just code exactly what you want. You can start with a structure that can hold either a character or an integer (with some boolean or integer to indicate which). Then you can allocate an array of them of any size.
When you read the first line, create an array of 5 such structures. Set their type variable to "char".
When you read the second lien, increase the size by 6. Set those six new ones to be integers.
And so on.
You can use an enum to track the type of each entry in the array. You can use a struct to hold the integer value, character value, (or just re-use the integer value) and type. You can make helper functions like isInteger, setIntegerValue, getCharacterValue, and so on.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
I would like to extract file names and their corresponding MD5 sum from a check sum file in a format such as this-
MD5 (FreeBSD-8.2-RELEASE-amd64-bootonly.iso) = 2587cb3d466ed19a7dc77624540b0f72
I would prefer to do this locally within the program, which rules out awk and the like.
You can read lines easily enough using fgets(). Don't even think of using gets().
If you're reasonably confident you won't be dealing with filenames containing spaces or close parentheses, you can use sscanf() to extract the bits and pieces:
char hash_type[16];
char file_name[1024];
char hash_value[128];
if (sscanf(line, "%15s (%1023s) = %127s", hash_type, file_name, hash_value) == 3)
...good to go...
else
...something went wrong...
Note the sizes specified in the sscanf() string compared to the variable definitions; there isn't an easy way to generalize that other than by using snprintf() to create the format string:
char format[32];
snprintf(format, sizeof(format), "%%1%zus )%%%zus) = %%%zus",
sizeof(hash_type)-1, sizeof(file_name)-1, sizeof(hash_value)-1);
Your alternative is some routine forward parsing to locate the hash type and the open parenthesis before the start of the file name, and some trickier backwards parsing, skipping over the hash value and finding the equals and the last close parenthesis, and then collecting the various parts.
You should be able to implement this with fopen(), fgets() and strchr() - but first you will need to nail down the format of the file more precisely (for example: what happens if the filename includes a ) character?)
I wouldn't advocate it in most languages, but why not just hit it up with POSIX regex?
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
I want to generate this sequential data in C:
data_packet[1] = 0706050403020100 (seed_value)
next
data_packet[2] = 0f0e0d0c0b0a0908
Next will be the next 8 hexadecimal characters and so on for say 100 bytes. How can I do it? Can we do it using character array?
You don't want to use a char array since char may or may not be signed (implementation defined). If you are playing with hexadecimal numbers from 0x00 to 0xFF, I strongly recommend using an unsigned char array.
Looks like the values in the array are sequential, from 0 to N. This indicates using a for loop.
The array is a fixed size of 8 bytes. Hmmm, another good candidate for a for loop.
The hard part of your task is the direction of the bytes. For example are filling in the array starting at position 7 or at position 0?
So here is some psuedo code to help you along:
For each value from 0 to N do:
begin
for array position from 0 to 7 do: // or from 7 to 0
put 'value' into the array at 'position';
call function to process the array.
end
For more fun, change the functionality of "put 'value'" to "put random value".
If you want us to actually write your program, let us know. I could use the extra money. :-)