# Thread: insert character to a string

1. ## insert character to a string

let's say i have a string "file.txt"
and i want to insert "_out" to make it look like "file_out.txt"

i don't know how to do this D:

2. How did you figure out the result was "file_out.txt" when you typed your question in this forum?

Whatever that process was, that's exactly the process you would use when programming too. All programming really is, is translating the method you use to solve the problem from your native language to whatever programming language you want. In this case it would seem from English to C. Notice, I emphasized the word you. That's because it's imperative that you understand how to solve a problem before you try to program a solution to it.

So once you can describe how you turned "file.txt" and "_out" into "file_out.txt" in English, then we can begin translating it to C.

3. If you are doing this a few different times, I suggest that you make separate strings for "file", "_out" and ".txt"

If you are unsure about the file name at runtime, you can use strtok to break up the file name and file extension.

I forgot to mention that you can put them together using sscanf

4. who can help me with this programs.and who can also do at least three or two of this program for me.

5.10 (Rounding Numbers) An application of function floor is rounding a value to the nearest integer. The statement

y = floor( x + .5 );

will round the number x to the nearest integer and assign the result to y. Write a program that reads several numbers and uses the preceding statement in a function called rounding to round each of these numbers to the nearest integer. For each number processed, print both the original number and the rounded number.

5.15 (Hypotenuse Calculations) Define a function called hypotenuse that calculates the length of the hypotenuse of a right triangle when the other two sides are given. Use this function in a program to determine the length of the hypotenuse for each of the following triangles. The function should take two arguments of type double and return the hypotenuse as a double.

5.16 (Exponentiation) Write a function integerPower(base, exponent) that returns the value of base raised to the exponent.

For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3.

Assume that exponent is a positive, nonzero integer, and base is an integer. Function integerPower should use for to control the calculation. Do not use any math library functions.

5.17 (Multiples) Write a function multiple that determines for a pair of integers whether the second integer is a multiple of the first. The function should take two integer arguments and return 1 (true) if the second is a multiple of the first, and 0 (false) otherwise. Use this function in a program that inputs a series of pairs of integers.

5.24 (Temperature Conversions) Implement the following integer functions:

a) Function celsius returns the Celsius equivalent of a Fahrenheit temperature.

b) Function fahrenheit returns the Fahrenheit equivalent of a Celsius temperature.

c) Use these functions to write a program that prints charts showing the Fahrenheit equivalents of all Celsius temperatures from 0 to 100 degrees, and the Celsius equivalents of all Fahrenheit temperatures from 32 to 212 degrees. Print the outputs in a neat tabular format that minimizes the number of lines of output while remaining readable

5. Originally Posted by marvens33161

who can help me with this programs.and who can also do at least three or two of this program for me.

5.10 (Rounding Numbers) An application of function floor is rounding a value to the nearest integer. The statement

y = floor( x + .5 );

will round the number x to the nearest integer and assign the result to y. Write a program that reads several numbers and uses the preceding statement in a function called rounding to round each of these numbers to the nearest integer. For each number processed, print both the original number and the rounded number.

5.15 (Hypotenuse Calculations) Define a function called hypotenuse that calculates the length of the hypotenuse of a right triangle when the other two sides are given. Use this function in a program to determine the length of the hypotenuse for each of the following triangles. The function should take two arguments of type double and return the hypotenuse as a double.

5.16 (Exponentiation) Write a function integerPower(base, exponent) that returns the value of base raised to the exponent.

For example, integerPower( 3, 4 ) = 3 * 3 * 3 * 3.

Assume that exponent is a positive, nonzero integer, and base is an integer. Function integerPower should use for to control the calculation. Do not use any math library functions.

5.17 (Multiples) Write a function multiple that determines for a pair of integers whether the second integer is a multiple of the first. The function should take two integer arguments and return 1 (true) if the second is a multiple of the first, and 0 (false) otherwise. Use this function in a program that inputs a series of pairs of integers.

5.24 (Temperature Conversions) Implement the following integer functions:

a) Function celsius returns the Celsius equivalent of a Fahrenheit temperature.

b) Function fahrenheit returns the Fahrenheit equivalent of a Celsius temperature.

c) Use these functions to write a program that prints charts showing the Fahrenheit equivalents of all Celsius temperatures from 0 to 100 degrees, and the Celsius equivalents of all Fahrenheit temperatures from 32 to 212 degrees. Print the outputs in a neat tabular format that minimizes the number of lines of output while remaining readable
1 - You are hijacking someone else's thread.

2 - Read this sites homework policy: No one will do your homework for you.

6. Originally Posted by Click_here
If you are unsure about the file name at runtime, you can use strtok to break up the file name and file extension.
I would probably use strchr, or even better strrchr, which allows you to search from the end of the string, which may be advantageous here. The returned pointer, if not NULL, can be set to '\0' and incremented, so you have the two parts.
Originally Posted by Click_here
I forgot to mention that you can put them together using sscanf
I think you mean sprintf.

7. k so my program is first you prompt the user to ask them for a file to open (choose 1 from many text files) using gets and store it in filename (let's say the file i want to open is file.txt)
then at the end when you want to print output to file, you want to add "_out" to the filename to make it "file_out.txt"
how to insert the _out to the existing string is where i'm stuck at

8. First, you need to better define exactly what it is you want to do. That is, describe the requirements in great detail, in English.
How do you know where to add the "_out"? Is it before the ".txt", or just before the ".", regardless of ".txt", ".csv", etc?
What do you do if there is no "." or ".txt", ".csv", etc?

Here is a (probably) incomplete list of input scenarios you should consider. Pretend I am the user, and I am running your program. What would the output file names be, given the following input file names? If the file name is invalid, say so. For example
Code:
```file.txt         file_out.txt
file.csv        ???
file            ???
file.foo.txt    ???
file.txt.txt    ???```
Once you know the requirements, know when and where to add "_out", then it will be much easier to figure out how to add it. When you finish defining requirements, and you get to the "how", don't worry about programming. You can't program a solution to a problem you can't solve. So you (as a human being) must be able to solve it first. So how would you solve this "on paper"? If I asked you to do this to a bunch of strings by hand, what would your process be so that you always produced the correct results?

Write down every little step you take in English. Something like:

1. Find the last '.' in the string
2. Write down everything up to, but not including, that dot in your "new string" (i.e. elsewhere on the paper)
3. ...

Once you have the process figured out in English, you can begin coding it. Each of those steps is the basis for your algorithm. Be methodical, code a small amount, test it and only move on once that aspect is working completely correctly.

I will tell you now, you will probably want two char arrays, one for the input file name, and one for the output. Make sure the output is big enough to contain any valid input, plus the "_out" addition, plus the trailing null character. You will also probably use one of the following functions: strrchr, strchr or strtok.

I will need to see some code, some effort on your part, before I assist you further.

9. Yes I did mean sprintf.

What have you tried so far?