Thread: codes

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    32

    codes

    Hi guys,
    i'm writing some codes for a coming exams and stuck with a strange question. in the question i'm asked to write 2 function as follows in the attached file.. and under the given limitations in the second file.
    i will start by saying that i can't use pointers at all.

    what i don't know how to do:
    1. space code- move each space one chars right. and in order ti revers the process we need to move each space chars left. how do i do that ?
    2. i didn't understand all the cleaning input thing... i have no idea.
    3. as well as for how to remove all spaces at the beginning and the end of a sentsnce.
    4. at the middle of sentence, the number os spaces needs to be reduced to one.

    thank you!
    Attached Images Attached Images codes-task-jpg codes-jpg 

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Cleaning the input.I am a user for your program.I input something and i hit
    whitespace whitespace whitespace a whitespace whitespace b whitespace c whitespace
    After cleaning the input what i typed should appear like this in your program
    a whitespace b whitespace c

    How you can do this?Read the whole line(you know how to ) in an array.Then check every single character and if it is a whitespace
    (array[i]==' ' ) then remove it if needed according to your hw

    EDIT:
    and about the space code follow the same logic as in cleaning input.Put the line into an array and check every single element of it.If capital replace it with the equivalent small letter(you might want to take a glance in ASCII table or do it with if -else statements.)If it is a space do what you have to do,if you read an a(for the irish),then read the next element(make sure you avoid overflow in last element) and if it is b remove it etc..
    Last edited by std10093; 08-30-2012 at 05:07 PM.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    how do i read it to an array ? can you show this part?
    and how do i remove ? by using what ?

  4. #4
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    1-reading an array.I thought we said on another post from you.. click me

    2-once you locate the whitespace for example,then shift every single element of the array from the end until the location of the element you want to be vanished.Actually we do not remove anything but all we do is overwritting on it!

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    about 2 :
    the problem is i don't how to write "locate the whitespace for example,then shift every single element of the array from the end until the location of the element you want to be ".
    and why from the end ?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This isn't quite clear to me, either. Sounds like, moving a space to the right a few char's - that would mean FIRST going to the end of the string, and moving every char one element to the right, for example. If the original string was:

    "Original stringhere", and you wanted to add a space between the 'g' and 'h' letters, you would have to go to the end of the string, and move each letter one to the right:

    Original stringher e
    Original stringhe re
    Original stringh ere
    Original string here -- done.

    a for loop would be good for this.

  7. #7
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    For the "Space code"-problem I would go through the string, reading character after character and everytime I come across a space swap it's position with the character to the right.
    You could do it forwards or backwards although I think starting at the end is a little bit easier because you don't have to modify the current index position after the swap.

    Bye, Andreas

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    of course.. i know it's a for loop. i just don't how to master the array's index to creat what they want. if for example we have this sentence: "i am you friend".. it will be : ia my ourf riend.

  9. #9
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    Perform the operation, character by character, on paper and note the steps you've taken.

    Soma

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by CproG100 View Post
    of course.. i know it's a for loop. i just don't how to master the array's index to creat what they want. if for example we have this sentence: "i am you friend".. it will be : ia my ourf riend.

    Do the exercise with paper and pencil a few times. Note the patterns as they become evident to you, after a few repetitions of it.

    Those patterns, when simplified into small steps, will become the backbone of your logic for the program.

    Code:
    for each char in the string, starting with the last (rightmost) char
       move every char, one index to the right, including the end of string 
       char: '\0', which you can't see when you print the string. 
       if the char is a space, move the next char after the space, to
       the right, one index. Then insert a space.
    end for
    Which should be OK, BUT it will be one one index over to the right, from where it should be:
    Code:
    Ia myou rfriend is the final goal, but
     Ia myou rfriend is what you'll wind up with
    Note that it's one space too long, on the right hand side, and the first char on the left, is a space - which needs to be removed.
    Fortunately, you have a function to remove leading spaces from the left hand side of strings. So use it, here

    Perfect?

    I haven't coded this up, so work it through, and code it up, and test it. It should be very close. Note that you will need one extra index number (length of the char array), to do this. In C, every group of letters or numbers, that is a string, MUST have an end of string char as it's rightmost char, to mark the end of the string. Any group of letters or numbers that DOES NOT have an end of string char at the end of it, is just a bunch of char's -- NOT a string.

    Note that the end of string char ('\0'), can also be a 0, but it can't be a '0', because a '0' is a zero char.
    Last edited by Adak; 08-31-2012 at 03:51 AM.

  11. #11
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    thanks!

  12. #12
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    o.k., quike question.
    i have 64 char size sentence input. i want to scan it and identify words that begin with a vowel (a , e, i , o, u). if it does, simply add "ay" to the end of the word. for example, "ear" becomes "earay". so, here is what i did:
    gets (str[64})
    for ( i=0; i< strlen(str); i++)
    if ... that the part that i got lost... how can i write for (or maybe "while") that says that a word begins with something ? and if i want to strat with a, e, etc.. i write a in ASCII , like this : 'a' ?
    and also, if i found a word that starts with a vowel and i want to add letter ti that word how do i write that ? str[i]= 'y'+ word ??

    thank you.
    Last edited by CproG100; 09-01-2012 at 04:59 PM.

  13. #13
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    maybe the first one should be : str[i]=='a' || 'e'|| 'i'|| etc
    str[i]= str[i]+'ay' is that correct ?
    i'm not sure about the 'ay'... that's not an ASCII code...

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by CproG100 View Post
    maybe the first one should be : str[i]=='a' || 'e'|| 'i'|| etc
    str[i]= str[i]+'ay' is that correct ?
    i'm not sure about the 'ay'... that's not an ASCII code...
    Correct.

    You have an input string of 64 char's, and IF the chars are a string, then there will be ONE (sometimes TWO) more chars, that you can't see, added onto the end of the chars.

    This is NOT a string - it's just a bunch of chars:
    I am not a string

    This IS a string, because it HAS the end of string marker on the end of it:

    I AM a string\0

    I'm printing the '\0' char this way, but conceptually, the '\0' char is a 0 (not a '0' which is the digit zero), and not any char that you can display on the screen). In many ways, it is by far the most important char in any string, because without it on the end, you have NO string, at all.

    When you declare a string in C, the newline is automatically appended onto the end of your chars, to make it a string:
    char mystring[]={"Row, row, row your boat, gently down the stream"};

    C makes this into:
    Row, row, row your boat, gently down the stream\0

    When you are working with just two chars, you can work with them as individual chars, if you want to - adding just one or two chars is not difficult, even if you have to be careful to replace the end of string char, afterward. When you have more chars to add to a string, then you need to use C's string functions to help out.

    If your char array is 64 chars long, and you have 63 or 64 chars already in it, then your char array isn't big enough to add 'a' and 'y' and '\0' into the chars. Here's what I recommend:

    Make your char array WAY over-sized - maybe 128 instead of 64 chars long. Now you have plenty of room to do your adding, moving, etc., without the danger of overflowing the array (easily).

    Use a variable like
    Code:
    int length;
    len = strlen(charArrayName);
    to help you keep track of where the end of string char is at, and where it needs to go.

    Note that gets() does NOT add an extra '\n' (newline char), to the strings it stores - but fgets() DOES add an extra '\n' (newline), to the strings that it stores. This trailing newline may need to be removed.

    Again, it's a tricky char, because you can't see it printed on the screen. You CAN see that it's there though - watch the cursor after you print the string. If the cursor stops right after the last letter of the string, then there is no newline. If the cursor moves down one row, and over to the far left hand column of the screen, then there is a '\n' newline present.

    To use string functions, like strlen(), etc., you need to #include <string.h> in your program.
    Last edited by Adak; 09-01-2012 at 09:38 PM.

  15. #15
    Registered User
    Join Date
    Aug 2012
    Posts
    32
    yes, sure u added #include <stdio.h>
    i got lost in your explanation a little bit... so can write str[i]= 'y'+ word ?
    and about the space code... how do i reverse the process? i mean to move each space chars left ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c codes need help,thanks
    By hth373737 in forum C Programming
    Replies: 21
    Last Post: 11-23-2008, 10:45 PM
  2. Need some C++ codes...
    By blah569 in forum C++ Programming
    Replies: 5
    Last Post: 10-05-2005, 02:21 PM
  3. zip codes
    By missny in forum C Programming
    Replies: 10
    Last Post: 09-16-2005, 02:39 AM
  4. VK codes
    By Hunter2 in forum Windows Programming
    Replies: 10
    Last Post: 08-24-2002, 09:58 AM
  5. converting scan codes to ascii codes
    By stupid_mutt in forum C Programming
    Replies: 11
    Last Post: 01-25-2002, 04:06 PM