Thread: inserting charcters in a string

  1. #16
    Registered User
    Join Date
    Apr 2004
    Posts
    210
    Quote Originally Posted by quzah
    Why not? Judges "earn" the right, by going to school and kissing enough asses to get elected or appointed, the right to kill people or lock them up, or decree they're insane, or...
    Not quite. They've been given the right by the legislative which happen to be the guys you voted for (well, for the most part). They don't earn the right by all their studies or asskissing - that just qualifies them for the job.
    Practically that might not make much of a difference... Anyways, I'm offtopic

  2. #17
    Registered User
    Join Date
    Nov 2003
    Posts
    28
    Well just for the record, as far as I know my solution to the original problem he had was not incorrect, other than the fact that I did not include the correct header. Unless that is, there are other probelsm that aren't showing up on the compiler I am using. As far as the rest of this debate, oh well I am over it.

    One last thing though, the compiler I am using is bloodsheds Dev C++, Are there any free compilers that any of you would recommend using instead of this one?

  3. #18
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Unless that is, there are other probelsm that aren't showing up on the compiler I am using.
    There are, but they're problems that most compilers either would not mention, or would only complain about in fascist mode. A more correct implementation would look like this:
    Code:
    #include <ctype.h>
    #include <stdio.h>
    
    int main ( void )
    {
      char input[] = "abcdefghijklmnopqrstuvwxyz";
      char test[] = "aeuio";
      char newname[sizeof input * 2 - 1];
    
      int x, y, v;
      int atvowel;
    
      atvowel = 0;
      v = 0;
    
      for ( x = 0; x < sizeof ( input ) - 1; x++ ) {
        for ( y = 0; y < sizeof ( test ) - 1; y++ ) {
          if ( input[x] == test[y] ) {
            atvowel = 1;
            break; /* no need to keep checking vowel is found */
          }        
        }
    
        newname[v++] = input[x];
    
        if ( atvowel == 1 && islower ( input[x] ) ) {
          newname[v++] = '#';
          atvowel = 0;
        }    
      }
    
      printf ( "%s\n", newname );
      getchar();
    
      return 0;
    }
    And an even better solution would utilize the standard library, thus removing a nested loop, a counter variable, and a flag:
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    
    int main ( void )
    {
      char input[] = "abcdefghijklmnopqrstuvwxyz";
      char test[] = "aeuio";
      char newname[sizeof input * 2 - 1];
    
      int x, v;
    
      v = 0;
    
      for ( x = 0; x < sizeof ( input ) - 1; x++ ) {
        newname[v++] = input[x];
    
        if ( islower ( (unsigned char)input[x] ) && strchr ( test, input[x] ) != NULL )
          newname[v++] = '#';
      }
    
      printf ( "%s\n", newname );
      getchar();
    
      return 0;
    }
    By using a function to search for the vowel, you can more easily see an easy optimization: placing the vowel search (a linear time operation) after the lower case test (a constant time operation), thus avoiding the test for a vowel if the character is not a lower case letter.

    >Are there any free compilers that any of you would recommend using instead of this one?
    No, that one will serve you well as long as you don't expect it to catch all of your errors. Not even Lint is a substitute for knowing what you're doing and using your brain.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM