Like Tree5Likes
  • 1 Post By anduril462
  • 1 Post By christop
  • 3 Post By laserlight

Remove uppercase case letters from a word

This is a discussion on Remove uppercase case letters from a word within the C Programming forums, part of the General Programming Boards category; Here's my code. Please help. Thanks in advance. Code: #include<stdio.h> int main(void) { char abc[] = {"MmyNnameIisMmonk"}; char *ptr = ...

  1. #1
    Registered User Sid_TheBeginner's Avatar
    Join Date
    Jun 2012
    Location
    India
    Posts
    19

    Exclamation Remove uppercase case letters from a word

    Here's my code. Please help.
    Thanks in advance.

    Code:
    #include<stdio.h>
    
    
    int main(void)
    {
        char abc[] = {"MmyNnameIisMmonk"};
        char *ptr = abc;
        int i = 0;
    
    
        for(i = 0; i < (sizeof(abc) / sizeof(char)); i++) {
            if(abc >= 'A' || abc <= 'Z') {
                ptr++;
                printf("%c ", ptr[i]);
            }
        }
        
    }
    Last edited by Sid_TheBeginner; 10-25-2012 at 11:35 AM.

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    639
    First of all
    Code:
    if(abc >= 'A' || abc <= 'Z')
    Look at that code and tell me what you think it does. What is abc pointing to each time through the loop? When is it being changed?
    Aside from that you should rearrange the A and the Z, and use strlen function in the string.h library. It returns the string length back to you, this will work well with your for loop.
    ex.)
    Code:
    for(i = 0; i <strlen(abc); i++)
    Don't forget to include the string.h library at the top
    Last edited by camel-man; 10-25-2012 at 11:52 AM.

  3. #3
    Registered User Sid_TheBeginner's Avatar
    Join Date
    Jun 2012
    Location
    India
    Posts
    19
    isn't the 'A' and 'B' used to pass throught the ASCII table values? Hence avoiding all the Uppercase letters? Thank You for your reply

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,631
    Code:
    if(abc >= 'A' || abc <= 'Z')
    Think about this carefully. Draw it out on a "number line" (letter line?):
    Code:
    . . . A B C D E F G H I J K L M N O P Q R S T U V W X Y Z . . .
           +------------------------------------------------------>    abc >= 'A'
    <-------------------------------------------------------+          abc <= 'Z'
    "OR" (||) means all the characters covered by either of the two conditions. That means every possible character.
    "AND" (&&) on the other hand, means only letters covered by both of the conditions. In this case, it would be only the uppercase letters.

    You really want the reverse of that, so think about what you should change ">=" and "<=" to. Hint, "<=" is not the opposite of ">=".

    But, you could just #include <ctype.h>, and use the isupper() function instead. Easier and more portable.
    rags_to_riches likes this.

  5. #5
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    639
    You need to refer to abc as, abc[i] instead of just abc in your for loop.

  6. #6
    Registered User Sid_TheBeginner's Avatar
    Join Date
    Jun 2012
    Location
    India
    Posts
    19
    The exercise demands to achieve it using math. I want to skip all the uppercase case letters.
    That means all characters between and including A and Z is it
    Code:
     (abc[i] >= 'A' || abc[i] <= 'Z') 
    Warning: a.c: In function ‘main’:a.c:40:17: warning: initialization makes pointer from integer without a cast [enabled by default]
    a.c:45:16: warning: comparison between pointer and integer [enabled by default]
    a.c:45:30: warning: comparison between pointer and integer [enabled by default]
    
    O/P : Segmentation fault(core dumped)
    Please Help.

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    639
    You should use strlen to calculate how many times your for loop should run. As well as refer to abc as abc[i] that way it will not always point to the beginning of the string.

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    383
    Quote Originally Posted by camel-man View Post
    You should use strlen to calculate how many times your for loop should run. As well as refer to abc as abc[i] that way it will not always point to the beginning of the string.
    It's not good practice to use strlen in a for loop as you posted because strlen will be called every time through the loop. This gives the loop O(n*n) time complexity. A better method is to save the return value of strlen and compare against that. Another good method is to check for end of string manually (check for null terminator). This last method avoids iterating through the string twice (once in strlen and once in the for loop).
    AndiPersti likes this.

  9. #9
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    639
    Ahh yes, that would be better. I am so rusty with strings, always forgetting about that null terminator

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One way:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void) {
       char abc[] = {"MmyNnameIisMmonk"};
       int i = 0, len;
    
       len = strlen(abc);
       for(i = 0; i<len; i++) {
          if(abc[i] >= 'a' && abc[i] <= 'z') {
              printf("%c", abc[i]);
          }
          else {
             if(i)
               printf(" ");
          }
       }
       printf("\n");
       return 0;
    }

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    75
    Quote Originally Posted by camel-man View Post
    Code:
    for(i = 0; i <strlen(abc); i++)
    )
    This is bad since strlen will be called for every character in abc.
    if abc is 1000 chars long, each of the chars in abc will be looked at 1000 times. thats 1000 * 1001 , 1 Million+ checks for the string terminator ('\0').

    sizeof(abc)/sizeof(char) is a const that will be evaluated once by most compilers , it's one char too long though.

  12. #12
    Registered User Sid_TheBeginner's Avatar
    Join Date
    Jun 2012
    Location
    India
    Posts
    19
    Thanks for all the help guys. I've come up with this:

    Code:
    #include<stdio.h>
    
    int main(void)
    {
        char abc[] = {"Mmy Nname Iis Mmonk"};
        int i = 0;
     
     
        for(i = 0; abc[i] != '\0'; i++) {
            if(abc[i] >= 'a' && abc[i] <= 'z') {
                printf("%c: %d ", abc[i], abc[i]);
            }
            else if(abc[i] == ' ') {
                printf("\n");        
            }
        }
        printf("\n");
    
    
        return 0;
    }
    
    O/P: 
    m: 109 y: 121 
    n: 110 a: 97 m: 109 e: 101 
    i: 105 s: 115 
    m: 109 o: 111 n: 110 k: 107
    Thought I'd learn some subnetting today and then go to college. But ended up doing this program
    Last edited by Sid_TheBeginner; 10-25-2012 at 08:57 PM.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,127
    Quote Originally Posted by Sid_TheBeginner
    The exercise demands to achieve it using math.
    You should be aware that the method that you use requires the letters of the alphabet to contiguous in value in the character set. This is very likely to be true these days since you will almost certainly be using ASCII or something incorporating ASCII, but in theory it is not guaranteed, hence isupper is a better option.

    Quote Originally Posted by Sid_TheBeginner
    I've come up with this:
    Instead of excluding uppercase letters, your program now includes only lowercase letters and spaces. If you're okay with this, well and good, but note the difference
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. out putting upper case letters
    By brillpsycho in forum C++ Programming
    Replies: 3
    Last Post: 10-10-2012, 06:06 PM
  2. Compare two string and remove common letters
    By NiLe in forum C Programming
    Replies: 4
    Last Post: 06-02-2012, 02:23 PM
  3. Find word with uppercase and lowercase letters
    By doia in forum C Programming
    Replies: 9
    Last Post: 07-15-2010, 09:51 PM
  4. Counting uppercase and lowercase letters in a text
    By Tintu in forum C Programming
    Replies: 2
    Last Post: 02-06-2008, 10:15 PM
  5. Remove upper case letters from a char string
    By lavinpj1 in forum C Programming
    Replies: 8
    Last Post: 05-01-2006, 01:09 PM

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