confused

This is a discussion on confused within the C++ Programming forums, part of the General Programming Boards category; I have to read in from input file and if a word in that file is longer than 7 letters ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    20

    confused

    I have to read in from input file and if a word in that file is longer than 7 letters insert "er" after the 3 letter of that word. I started by reading the file into a cstring. I know how to find the length of the words I just cant figure out how to insert "er" into the word.

    any ideas?

    thanks

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    ok.. so how are you finding the length.. have any code..

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    Code:
    ifIn.getline(szTemp,20,' ');
    iLeng=strlen(szTemp);
    if(iLeng==7)
    {
      //do something
    }

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Should be if(iLeng > 7) instead of if(iLeng==7) according to your description.
    To merge one string at the end of another, use strcat().
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    of course there are many different ways to do it, but what I suggest is that you make use of these two functions:
    strncpy() and strcat()

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    Code:
    .
    .
    if (iLeng>7)
    {
      for(int i=0; i<=2; i++)
      {
        ifIn>>temp[i];
      }
      strcat(temp,"er");
    
    // I thought about this but i don't know what to do here
    but if i do it this way i don't know if it will work for every word in the file because if I loop this for every word in the file my "i" will be at a different point in the file.

    so this is where i get confused

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    34
    What does Your Variable Temp[i] hold?


    If length of the word >7 then
    word=word+er

    to make word=word+7 you can use the strcpy, strcat functions

  8. #8
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Originally posted by ddavidson
    If length of the word >7 then
    word=word+er

    to make word=word+7 you can use the strcpy, strcat functions
    Actually that won't accomplish his goal. He needs to:
    insert "er" after the 3 letter of that word
    The resultant string should be src[0-2]+"er"+src[3-len]

  9. #9
    Registered User
    Join Date
    Apr 2003
    Posts
    20
    but, how do I code it so it will work for every word in the file that I need it to. because i am clueless

  10. #10
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    The steps:
    Code:
    1) read a word from the file
    2) if word is > 7
      a) copy the first 3 characters to a separate buffer
      b) concatenate "er" to the separate buffer
      c) concatenate the 4th to the end of the word to the separate buffer
    3) do whatever else you need to do with the buffer (display it, etc)
    4) repeat step 1 and continue until end of file

  11. #11
    Registered User
    Join Date
    Feb 2003
    Posts
    34
    Ooops

    My Bad!!

    Here is a hint for you

    S3.insert(3, s4, 0, string::npos); will insert "s4" at location 3 in the string s3.


    In other words You insert in your case "er" before s3's third letter

    The last two arguments specify the start of the word and the number of characters from s4 that should be inserted in in your case:2

    So all you need to find is where to insert the "er"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Confused with array size
    By desmond5 in forum C Programming
    Replies: 4
    Last Post: 12-04-2007, 05:14 PM
  2. New to C++ and confused by boolean and ifs and else
    By jconner in forum C++ Programming
    Replies: 10
    Last Post: 08-02-2006, 04:29 AM
  3. why wont this compile?!? :confused:
    By jdude in forum C++ Programming
    Replies: 5
    Last Post: 11-25-2004, 01:13 AM
  4. confused.. in selecting my line of deapth
    By jawwadalam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-04-2003, 02:21 PM
  5. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 11:11 PM

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