Like Tree3Likes
  • 3 Post By Click_here

insert character to a string

This is a discussion on insert character to a string within the C Programming forums, part of the General Programming Boards category; let's say i have a string "file.txt" and i want to insert "_out" to make it look like "file_out.txt" i ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    9

    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. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    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. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    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
    Fact - Beethoven wrote his first symphony in C

  4. #4
    Registered User
    Join Date
    Feb 2013
    Posts
    1

    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. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    Quote Originally Posted by marvens33161 View Post

    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.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Quote Originally Posted by Click_here View Post
    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.
    Quote Originally Posted by Click_here View Post
    I forgot to mention that you can put them together using sscanf
    I think you mean sprintf.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    9
    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. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    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. #9
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,323
    Yes I did mean sprintf.

    What have you tried so far?
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insert a character in a string
    By madmars in forum C Programming
    Replies: 9
    Last Post: 02-09-2011, 01:57 PM
  2. error from string insert
    By bvkim in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2010, 08:43 PM
  3. Insert into string at specified index?
    By RedZone in forum C Programming
    Replies: 2
    Last Post: 07-24-2006, 07:01 PM
  4. Insert int into string
    By lpmrhahn in forum C Programming
    Replies: 1
    Last Post: 04-09-2004, 02:10 PM
  5. Unable to insert a string into another string
    By Jacquiline in forum C Programming
    Replies: 0
    Last Post: 04-05-2003, 08:15 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21