Thread: C assignment question

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    12

    C assignment question

    Hey guys just need a bit of help with some code i am doing for an assignment. Basically we are given a file and we need to manipulate the data. However there is one thing im struggaling to do. I need to make any occurence of the letter "i" on its own a capital.

    The way i am doing the rest is loading each char with:

    while(fgets(t, 2, stream)!=NULL)

    and using if statements with strchr to check the char. The problem is any occurence of i on its own needs a space before and a space after. I can recognise the space before but not sure how to do the space after.

    Any help please ?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    It would probably be easier if you read in an entire line at a time, then used strchr (or just iterate straight thru the line), rather than working byte by byte during input.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    What if there is multiple occurences of a char on a single line?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Notice that strchr returns a pointer. You can reuse this pointer to search the rest of the string until strchr finally returns a null pointer indicating that the character could no longer the found.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    Ok how do i use fgets to read a line instead of char?. Only started c a few months ago and our lecturer kinda...sucks :P

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Martyn27 View Post
    What if there is multiple occurences of a char on a single line?
    Well, you want to process the entire line, yeah. Something like:
    Code:
    	char str[]="the quick brown i jumps over the lazy i.\n", *ptr = str;
    	while ((ptr = strchr(ptr,'i'))) {
    IMO this is going to be more awkward, it is easier to just go:
    Code:
    	char str[]="the quick brown fox i jumps over the lazy dog i.\n";
    	int x, len = strlen(str);
    
    	for (x=0;x<len;x++)
    		if (str[i] == 'i' &&
    I left that incomplete because you will need a more complete and complex set of conditions -- eg, if "i" must have a space on either side, then the for might as well be:

    for(x=1;x<len-1;x++);

    And you can just check to see that str[i-1] and str[i+1] are spaces. But I thought I'd also point out that if you have an i outside of a word at the beginning or end of the line, does that count (eg "the lazy dog i.\n")?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    Ok thanks ill give that a try

    Yeah. The brief says any occurence of the char i on its own should be made Capital.

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    Argh. To get it to read line by line it means im going to have to change my entire program. Would there be anyway possible to do it by reading one char at a time?

  9. #9
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    No and for one simple reason: reading one character at a time will require a Kreskin - Wikipedia, the free encyclopedia library to know what is coming next (ie ' ') though you can easily keep track of what came previously. More than that though is the gotcha involved with the situation where 'i' appears at the end or beginning of a line; the next or previous character is not going to be a space yet you will probably be required to recognize that it is a lone 'i' character. In a case line this it would probably pay to use whats called a sliding window buffer as opposed to a character by character or even line by line examination of the data. Say your data looks like this:

    I have no way of knowing when
    she will come home but I
    am sure she will someday.

    This would absolutely defeat both methods described. A sliding window buffer would look at (say) 3 characters at a time like:
    [] denotes buffer
    [I h]ave no way of knowing when\nshe will come home but I\nam sure she will someday.
    Now you just look at the buffer:
    Is the previous character whitespace/beginning of file? is the next? if so, capitalize.
    Now move your buffer by one:
    I[ ha]ve no way of knowing when...etc
    So you keep sliding the window through the data and only look at the three characters in the window. This way you can be sure of catching stuff broken between lines/buffer edges, etc.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Martyn27 View Post
    Argh. To get it to read line by line it means im going to have to change my entire program.
    You mean all 20 lines?

    I'm not trying to belittle, just:

    1) It cannot in reality mean changing "everything completely and starting from scratch" unless all the program does so far is open a file and read in some bytes.

    2) This is programming. You can't recognize you've made a mistake and decide to forget about it. Even if it takes days to fix.

    3) Here's a lesson about "modularity": if you write short discrete functions that accomplish a specific task in relation to the whole context, it is much easier to modify the parts. You should be writing with the awareness that each part needs to relate to the other parts in such a way that one part can be easily changed without having to change "the entire program". That is REALLY REALLY REALLY a fundamentally important principle.

    So, a few hours (or, this case, I think more like a few minutes) rewriting will help impress this upon you. That's what I always say to myself...and it's true. I can read something somewhere and totally forget it. Then I remember, when it's too late and I'm having to deal with the consequences BUT my chances of forgetting after "learning the hard way" are much much less.

    Anyway, you can always post your whole program and maybe someone will have some further insight WRT all this.
    Last edited by MK27; 02-23-2010 at 02:32 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    Yeah but its doing other stuff for example
    Code:
     else if (strchr(c, '#'))								 // Check for a hash.
       {
    	correctionCount++;
       }
    //-------------------------------
     else if  (strchr(c, ' '))									  // Check for a space.
       {
    	printf(" ");
    	wordCount++;
    	correctionCount++;
       }
    Got it removing jibberish from a file etc.

    If i change the way it reads wont it affect the things like this?

  12. #12
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    Or i could just add another loop at the end i guess. Keep that loop what its doing and use this one to make all I's a capital.

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay you are parsing. Don't you see how it is easier to do this:

    - read in one complete line.
    - iterate thru the line one element at a time.

    Some advantages: you don't have to do a string of else (strchr...) stuff. You can go:
    Code:
    int i, len = strlen(line);
    for(i=0;i<len;i++) {
         switch (line[i]) {
              case ('#'): 
                   	correctionCount++;
                    break;
             case (' '):
                    printf(" ");
    	        wordCount++;
    	        correctionCount++;
                    break;
            case ('i'):
                    if (line[i-1] == ' ' && line[i+1]) line[i] = 'I';
                    break;
    If you haven't used switch/case before and aren't suppose to, you can use the if/else.

    Nb, using strchr() on a single character is EXTREMELY silly. Just compare (if c == ' ').
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Oct 2009
    Posts
    12
    Ok ill try it that way. What should i use to get the data from a file fgets still? Yeah we have used switch/case but briefly.

    But still, arent i going to have a case for every type of illegal character?

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    fgets() is perfect.

    You can have as many cases as you want. Remember, every case should end with "break".
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using swap to make assignment operator exception safe
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2008, 06:32 AM
  2. array assignment question
    By threahdead in forum C Programming
    Replies: 2
    Last Post: 08-17-2003, 05:36 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. Replies: 2
    Last Post: 12-17-2001, 06:40 PM