Thread: Replacing lowercase letter with the '@' symbol

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    3

    Replacing lowercase letter with the '@' symbol

    Hey guys, I'm new to this forum and am just starting to learn C programming. I have a couple questions for my assignment and was wondering if I could get some help.

    a) Write a C program that reads characters from standard input and replaces every lowercase a with the "at" symbol (@) before outputting the characters to standard output. For example, if your program is called a2at, the you could use the program as follows

    eg. $ echo "This is an easy problem" | ./a2at
    This is @n e@sy problem

    b) Write a C program that reads from standard input and counts the number of uppercase letters. If your program is called count_upper, the you could use it as follows

    eg. $ echo "Also an EASY problem" | ./count_upper
    5

    Any help would be greatly appreciated. Thank you.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    I have a couple questions for my assignment and was wondering if I could get some help.
    No you have provided absolutely *zero* questions. Instead you've just dumped your assignment on the board, apparently expecting us to do it for you. You might want to read this: Announcements - General Programming Boards

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    He clearly asked for help, not for us to do it for him. Greeting a first timer on the board, I hope your welcome will be as warm as your reply was not ^^^^.

    We do get a lot of that "let me dump this on you and hope you'll do it for me", kind of posts. Hope you understand.

    Welcome to the board, GWST11, and let's start with the first problem, only. What are you stuck on?

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Code:
    #include <ctype.h>
    
    /* ... */
    
    for (/* */) {
        if (islower(/* */)) {
       
        }
    }
    This might help you get started...

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    Thank you guys for the replies. Obviously my intention was not to have you guys do the assignment for me, I understand the rules and ethics, so I apologize if I sounded that way. I think I asked the question way too early, so I'm going to do more tutorials and continue practicing. And if I get really stuck then I will come back to this board, thanks for the head start memcpy.

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    3
    a) Write a C program that reads characters from standard input and replaces every lowercase a with the "at" symbol (@) before outputting the characters to standard output. For example, if your program is called a2at, the you could use the program as follows

    eg. $ echo "This is an easy problem" | ./a2at
    This is @n e@sy problem

    THIS IS WHAT I HAVE SO FAR:

    Code:
    #include <stdio.h>
    #include <ctype.h>
        void main()
        {
        int i =0;
            char s[1024];
            char c;
            int n;
    
     while( scanf( "%c", c))
     {
     s[i] = c;
    i++;
    }
    }
    
    for (i = 0; i < strlen(s); i++)
    {
    c = s[i];
    
    if (islower(c))
    {
     n = n + 1;
    }
    printf("number of upper case letters is %d", strlen(s) - n);
    }
    I am stuck now, I don't what the problem is, but maybe it has something to do with the strlen? Can anyone give me a hint, thanks.

  7. #7
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    You did not initialize n to zero.
    The input loop should be something like
    Code:
    while (1) {
        scanf("%c", &c);
        if ('\n' == c)
            break;
        s[i] = c;
        i++; }
    You need to put in a null terminator:
    Code:
    s[i] = '\0';
    The ouput of number of upper case letters should be outside the loop.
    You forgot to substitute the '@' in places where there is lowercase letter.
    You forgot to output the string 's'.
    Last edited by nonoob; 02-14-2012 at 02:19 PM.

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    88
    Quote Originally Posted by GWST11 View Post

    Code:
    ...
    for (i = 0; i < strlen(s); i++)
    {
    c = s[i];
    
    if (islower(c))
    {
     n = n + 1;
    }
    printf("number of upper case letters is %d", strlen(s) - n);
    }

    Using strlen like that is bad.

    since strlen function is something like
    Code:
    int 
    strlen(const char *s)
    {
       const char *b =s;
       while(*b)
          b++;
       return(b-s);
    }
    so if your string is 100 chars long then your for(i=0;i<strlen(some_string); i++ ) will look at all 100 chars 100 times for a total of 10K looks just from the strlen.




    its much better to use some thing like
    for(i=0;some_string[i]; i++)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-10-2012, 08:49 PM
  2. Count lowercase letter
    By krakatao in forum C Programming
    Replies: 23
    Last Post: 11-16-2011, 12:01 AM
  3. Lowercase letter frequency
    By pauzza in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 12:32 PM
  4. Big Letter became small letter
    By cogeek in forum C Programming
    Replies: 27
    Last Post: 12-13-2004, 02:04 PM
  5. Replacing a Decimal with a letter in a numeric variable.
    By Dobermin in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2003, 01:38 PM