answersLogoWhite

0

The standard library is your friend here - look for 'strlcpy'

Without error checking, it should look something like this...

// START CODE

// we need an extra char

// for the null character at the end - so we need j + 1 chars

char * s2 = malloc( sizeof(char) * (j + 1 ) );

strlcpy( s2, s1 + j, j + 1 );

// END CODE

Hope this helps.

EDIT:::::::

Oops... strlcpy is not actually part of the C standard library... :(

It ought to be kicking around on most Linux distros...

If your environment does not provide strlcpy, use its cousin, strncpy

to do this with strncpy... (strncpy has poorer error handling than

strlcpy imo, but they operate very similarly)

// START CODE

char * s2 = malloc( sizeof( char ) * ( j + 1 ) );

strncpy( s2, s1 + j, j + 1);

s2[j] = '\0'; // strncpy does not guarantee a null-terminated result :(

// END CODE

Hope this helps - and isn't broken ;)

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

What is substr function does in c?

it will give the substring of length(as per the user) from the actual string,and the starting length from where it has to copy the substring from actual should be given by the user.


How do you use sub string in java?

Substring method creates smaller string form bigger string , every time a new string is created but original character array buffer is used. so even if original string is 1G in size and substring is just 1K memory held by substring is 1G because of backed array. this can cause memory leak and prevent original string from garbage collection even if there is no direct reference.


How do you show the location of an occurrence of the letter e in a string using javascript?

The location of a substring within a string in JavaScript can be found using the indexOf() method of the string type. That method will return the placement of a string as a numerical index, starting from 0. The method takes two arguments. The first is the substring you're looking for. In this case "e." The second argument is optional, and it's the "start" argument. If you define it, you define the index from which JavaScript will start looking for the substring. By default, this index is 0 (zero.) If you want to find all of the occurrences of a string using this method, you have to write a loop. To see a working example of that, check out the JSFiddle in the related links. I've also attached a good lesson on JavaScript strings in PDF.


This a prewritten function that is built into a programming language?

Library Function Starting out with Programming Logic and Design by Tony Gaddis Page 218


How do you use fseek function in c?

AnswerFirst a few points.I do not know if you intended it but you are asking this of a C++ expert not a C expert. I mention this as fseek forms part of the standard C library file operations. Of course the C++ standard library inherits these functions, but they are not directly compatible with C++ IOStreams. You therefore have to have opened the file using the C fopen function, and close it using the C fclose function.Next, you have to be careful when using fseek with text files. Things like end of line character sequences and the size of characters used can make a difference to the position values you need to pass to fseek via its offset parameter. The fseek function positions to the requested byte position within the referenced file, it knows and cares nothing about characters or their encoding making up the data of the file.You also have all that silliness to do with character 26 (ctrl-Z) being interpreted as end of file in text mode - a Microsoft hangover from the days of MS-DOS. Then there is the effect caused by skipping over the byte order mark (if there is one), which as far as I know has to do with text files of characters in various Unicode encodings (so hopefully can be ignored in simple cases). See http://unicode.org/unicode/faq/utf_bom.html#BOM for more information on byte order marks.You ask about positioning to the letter positions. Not all characters are letters and not all characters are printable. As I said fseek knows and cares nothing about the data or meaning of the values in a file and therefore cannot tell a letter from any other character type.So assuming you meant characters when you mention letters in your question and assuming characters are all the same size and are byte sized then to position to the 45th character when starting with the file pointer positioned at the 20th character you can do the following:status = fseek( file, 44L, SEEK_SET );The fseek call shown above assumes file is a FILE * pointing to a valid open FILE structure. 44L specifies the position as a long integer constant, SEEK_SET is a value indicating that 44L is used to specify the position counting from the beginning of the file. As the beginning of the file is at position 0 and not position 1 we have to subtract 1 from the byte number we want if we count the first character as character 1.The status variable is an int and is used to hold the returned value which is 0 on success and some non-zero value if fseek failed. If fseek fails you should check errno for more details or call a function such as perror as in the MSDN example, which converts the errno value to a string, prefixes the passed text and displays it on stderr (as associated with std::cerr in C++).Alternatively you could specify the position to move to relative to the current position which would be the value given by the difference between the new position from the current position, thus 45-20 = 25:status = fseek( file, 25L, SEEK_CUR );Here the way the position value is interpreted is specified as SEEK_CUR, meaning to count from the current file position. This assumes of course that the file pointer position is in fact at position 19 (i.e. the 20th byte counting from 0) to start with. If it were at (say) position 112 then it would end up at position 137.The reverse would be either:status = fseek( file, 19L, SEEK_SET );That is move to position 20 as counted from 0, the beginning of the file.Or:status = fseek( file, -25L, SEEK_CUR );Which means move to the current position less 25, which assuming the file pointer is positioned at position 44 (i.e. byte 45 counted from 0) to start with would position it to position (44-25) = 19 (i.e. byte 20 counted from 0).If you look carefully at the signature for fseek then you would notice that the offset parameter is specified as a long value - that is it is a signed value and so can be negative.I should note that we could also position relative to the end of the file by specifying SEEK_END as the third parameter value. However this would mean we would need to know how long the file was in bytes so we can calculate the distance from the end of the file to the positions we require and so seems to be more effort than it is worth in this case.Hope this helps. If you require further information then please ask another question.

Related Questions

What is th function of the charindex?

The CHARINDEX function in SQL is used to find the position of a specific character or substring within a string. It returns the starting position of the substring or character within the given string.


How would you implement a substr function that extracts a sub string from a given string?

substr(string, position [, count]) It extract substring starting from start and going for count characters. If count is not specified, the string is clipped from the start till the end


What is substr function does in c?

it will give the substring of length(as per the user) from the actual string,and the starting length from where it has to copy the substring from actual should be given by the user.


How do you use sub string in java?

Substring method creates smaller string form bigger string , every time a new string is created but original character array buffer is used. so even if original string is 1G in size and substring is just 1K memory held by substring is 1G because of backed array. this can cause memory leak and prevent original string from garbage collection even if there is no direct reference.


Is formula and a function are they same in Excel?

A function is one specific function, like LEFT for getting a substring starting from the left, or TODAY for getting today's date, or SUM for getting the sum of some numbers. A formula is a combination of cell references, values, strings, and functions, used to define how to calculate the value to display in a cell.


The distance between the final position and the starting position is the?

The distance between the final position and the starting position is the


What is the CHARINDEX function used for?

The CHARINDEX function is used to search for an expression inside another expression and returns to the starting position if one is found. The CHARINDEX function contains the following 3 arguments expressionToFind, expressionToSearch, and start_location.


What are characters starting with the letter h?

One question What type of characters?


What is the function of equals MID in Microsoft Excel?

The MID function is used to extract some text from the middle of an existing piece of text. If you wanted to get characters from the start of text, you can use the LEFT function, but that is not sufficient if you wanted to start extracting text from the middle of a piece of text. Using the MID function you can start at any point in the text and get an amount of characters from it. You specify the text, then the starting point and then the amount of characters you want. For example, the following will give you BCD by starting at the second character and getting three characters from it. =MID("ABCDEF",2,3) There are lots of times MID can be useful. Sometimes you can use the find function to find a character to start from, like finding the first space, and starting from there. So suppose you have a list of names that include titles like Mr. or Mrs. and you want to find the first letter of the surname. That would require finding the first space and then the next character. So if you had Mr. Smith in cell B2, to extract the S you would use the MID function and the FIND function like this: =MID(B2,FIND(" ",B2)+1,1)


What is the function of air-starting valve?

is the starting valve


What is the queen's starting position in chess?

The queen's starting position in chess is on the square d1 for White and d8 for Black.


Fictional characters starting with x?

Xena