Thread: convert from upper to lower

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    24

    convert from upper to lower

    Hello
    please
    1-what is the proplem with this code
    2-how can i use fgets in this code
    3- can i use BUFSIZ
    which will be more faster BUFSIZ or strlen()

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdbool.h>
    char toupper(char);
    char tolower(char);
    bool islower(char);
    bool isupper(char);
    int main(){
      char str[20];
      int i;
      printf("Enter any string:");
      gets(str);
      for(i=0;i<=strlen(str);i++){
                if(islower(str[i])==1)
                toupper(str[i]);
    
    
        else if(isupper(str[i])==1)
    
                tolower(str[i]);
    }
        printf("%s\n",str);
      return 0;
    }
    
    bool islower(char ch){
    
    if(ch>='a'&&ch<='z')
        return 1;
    else return 0;
    }
    
    bool isupper(char ch)
    {
        if (ch<='A' && ch>='Z')
                return 1;
                else return 0;
    }
    
    
    char toupper(char ch) {
            if (ch <= 'a' && ch >= 'z')
              ch=ch+32;
            return ch;
    
    }
    
    char tolower(char ch) {
            if (ch <= 'A' &&ch >= 'Z')
                ch=ch-32;
            return ch;
    
    }
    Last edited by tost; 02-04-2013 at 02:32 PM.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    What is BUFSIZ? A macro of stdio.h.
    strlen is a function. So, I would go for strlen. As a matter of fact, I would cache the result of it, before going to line 13.
    In code, you have
    Code:
    for(..;i<strlen(..);..)
    I would have
    Code:
    int len = strlen(...);
    for(.. ; i < len ; ..)
    because you don't need every time the loop is executed the strlen function to be invoked again and again

    >What is the program with this code?
    What do you think it is?

    You must use fgets! gets must not be used.
    What you should do, is to use fgets instead of gets.
    Last edited by std10093; 02-04-2013 at 02:52 PM. Reason: ref of fgets
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    i used fgets and made new var for strlen()
    but the same proplem

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdbool.h>
    char toupper(char);
    char tolower(char);
    bool islower(char);
    bool isupper(char);
    int main(){
      char str[20];
      int i;
      printf("Enter any string:");
      fgets(str,20,stdin);
      int len=strlen(str);
      for(i=0;i<len;i++){
                if(islower(str[i])==1)
                toupper(str[i]);
    
    
        else if(isupper(str[i])==1)
    
                tolower(str[i]);
    }
        printf("%s\n",str);
      return 0;
    }
    
    bool islower(char ch){
    
    if(ch>='a'&&ch<='z')
        return 1;
    else return 0;
    }
    
    bool isupper(char ch)
    {
        if (ch<='A' && ch>='Z')
                return 1;
                else return 0;
    }
    
    
    char toupper(char ch) {
            if (ch <= 'a' && ch >= 'z')
              ch=ch+32;
            return ch;
    
    }
    
    char tolower(char ch) {
            if (ch <= 'A' &&ch >= 'Z')
                ch=ch-32;
            return ch;
    
    }


    thanks for help

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    It would help us, and you, immensely if you actually told us what the problem is!

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    i need program that i input string like this
    TOSst!@
    and output
    tosST!@

    the proplem in source that if write anything it just output it the same

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Your "toupper()" and "tolower()" functions return the modified character value. Therefore, in "main()", you want to assign the result of those functions to the current string index.

    Also, you have redundant checks in your "toupper()" and "tolower()" functions (they're already checked in your "isupper()" and "islower()" functions).

    Another thing - carefully go through your code and double-check all <= and >= operators; you seem to have made a few mistakes with those.

    And one last thing - those function names are already reserved, so if you're writing your own versions, make the names unique.

    Code:
    main.c|4|warning: conflicting types for built-in function 'toupper'|
    main.c|5|warning: conflicting types for built-in function 'tolower'|
    main.c|6|warning: conflicting types for built-in function 'islower'|
    main.c|7|warning: conflicting types for built-in function 'isupper'|
    ||=== Build finished: 0 errors, 4 warnings ===|

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by tost View Post
    i need program that i input string like this
    TOSst!@
    and output
    tosST!@
    Wait! How did YOU do this? HOW did you change TOSst!@ to tosST!@?

    The steps you took to do it, are the same steps that the computer might take to do it, (with just a few wrinkles).

    Study how you did it, and write down each little step of logic you took, and THAT can be the same steps of logic your program can take, as well.


  8. #8
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    Thanks i forgot that c++ has toupper tolower function so i changed all name
    i removed the second check in to_lower to_upper
    and <= >=;

    could you tell me what IDE or compiler you use?
    i still have the same proplem

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdbool.h>
    char to_upper(char);
    char to_lower(char);
    bool is_lower(char);
    bool is_upper(char);
    int main(){
      char str[20];
      int i;
      printf("Enter any string:");
    fgets(str,20,stdin);
      int len=strlen(str);
      for(i=0;i<len;i++){
                if(is_lower(str[i])==1)
                to_upper(str[i]);
    
    
        else if(is_upper(str[i])==1)
    
                to_lower(str[i]);
    }
        printf("%s\n",str);
      return 0;
    }
    
    bool is_lower(char ch){
    
    if(ch>'a'&&ch<'z')
        return 1;
    else return 0;
    }
    
    bool is_upper(char ch)
    {
        if (ch<'A' && ch>'Z')
                return 1;
                else return 0;
    }
    
    
    char to_upper(char ch) {
              ch=ch+32;
            return ch;
    
    }
    
    char to_lower(char ch) {
                ch=ch-32;
            return ch;
    
    }

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    mr.Adak
    i will let user input in char array and i will check every char if it upper then change to lower if it lower change to upper and the ascii symbol normal
    that's my logic can you write to me how you think when you get programming proplem ?
    i mean now i have proplem like 123456 and i need to get it like 1 2 3 4 5 6
    do not write the program please but just tell me how you think about that?

  10. #10
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    As matticus said, you have to retrieve what your functions return in main (otherwise, they are "lost").
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Matticus View Post
    Your "toupper()" and "tolower()" functions return the modified character value. Therefore, in "main()", you want to assign the result of those functions to the current string index.
    That's how you would update the string in "main()."

    --------

    Code:
    bool is_upper(char ch)
    {
        if (ch<'A' && ch>'Z')
                return 1;
                else return 0;
    }
    You need to think about the logic. You seem to want this function to return 1 if the character is uppercase, but your logic says "return 1 if the character is less than 'A' and greater than 'Z'" (which is impossible, by the way).

    Also, on the standard ASCII chart, lowercase letters come after uppercase letters, so to make a lowercase into an uppercase, you would have to subtract (not add) a value.

    --------

    Take a deep breath, slow yourself down a bit, and go through your code slowly and thoroughly, making sure you understand what each line of code is doing.

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    24
    mr std10093
    i red the topic about why not using BUFSIZ it's size changeable from compiler to other
    so where can i use it socket programming or what ?
    i mean best situation to use BUFSIZ an nothing else

  13. #13
    Registered User
    Join Date
    Nov 2012
    Posts
    24

    that's right no update happen in the main function it just change the value then nothing
    The correct code
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdbool.h>
    char to_upper(char);
    char to_lower(char);
    bool is_lower(char);
    bool is_upper(char);
    int main(){
    
      char str[20];
      int i;
      printf("Enter any string:");
      fgets(str,20,stdin);
      int len=strlen(str);
      for(i=0;i<len;i++){
                if(is_lower(str[i])==1)
                str[i]=to_upper(str[i]);
    
    
        else if(is_upper(str[i])==1)
    
                str[i]=to_lower(str[i]);
    }
        printf("%s\n",str);
      return 0;
    }
    
    bool is_lower(char ch){
    
    if(ch>=97&&ch<=122)
        return 1;
    else return 0;
    }
    
    bool is_upper(char ch)
    {
        if (ch>=65 && ch<=90)
                return 1;
                else return 0;
    }
    
    
    char to_upper(char ch) {
    
            return ch-32;
    }
    
    char to_lower(char ch) {
    
            return ch+32;
    
    }
    thanks
    mr.Matticus

    mr.Adak

    mr.std10093


    now i have this Q
    1-what does mr.std10093 mean by?
    It’s 2013 and I still use printf() for debugging.

    2- best situation that i use BUFSIZ MACRO ?

    3-Q like this 12345 need to out put 1 2 3 4 5
    how can i think in problem like this ?



    Realy thanks for help

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by tost View Post
    Code:
    bool is_lower(char ch){
    
    if(ch>=97&&ch<=122)
        return 1;
    else return 0;
    }
    
    bool is_upper(char ch)
    {
        if (ch>=65 && ch<=90)
                return 1;
                else return 0;
    }
    
    
    char to_upper(char ch) {
    
            return ch-32;
    }
    
    char to_lower(char ch) {
    
            return ch+32;
    
    }
    No, don't do that. Why did you change it from the perfectly descriptive 'A' and 'Z' etc to 65 and 90?
    You'd best change those back.

    Also, that magic number 32 is bad as well. You could instead return ch + 'A' - 'a'; for to_upper, for example.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by tost View Post
    mr.Adak
    i will let user input in char array and i will check every char if it upper then change to lower if it lower change to upper and the ascii symbol normal
    that's my logic can you write to me how you think when you get programming proplem ?
    i mean now i have proplem like 123456 and i need to get it like 1 2 3 4 5 6
    do not write the program please but just tell me how you think about that?
    123456 - The input

    My thinking goes like this:

    #1.
    for every digit in the set of digits, print it, then print a space

    #2.
    for(every digit in the set of digits)
    ....print the digit and a space

    #3.
    for(i equals first digit, i is less than or equal to 6, increment i)
    ....printf(the digit and 1 space)

    Ready to code up the loop.

    It's odd to take it through these steps, because with practice, you just say "got it", and code it. But the process REALLY comes into play with any problem where you don't have an answer.

    Failing anything better, start with how YOU would do it, and take that logic step by step, to your program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String upper-lower case
    By icor15 in forum C Programming
    Replies: 1
    Last Post: 11-26-2012, 03:00 AM
  2. to lower or to upper that is the question!
    By verbity in forum C++ Programming
    Replies: 20
    Last Post: 04-25-2007, 06:42 PM
  3. Lower to Upper
    By Krush in forum C Programming
    Replies: 13
    Last Post: 11-19-2002, 10:14 PM
  4. string converting upper/lower
    By jlamn in forum C Programming
    Replies: 9
    Last Post: 09-24-2002, 06:01 PM
  5. lower to upper
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 07-29-2002, 07:50 PM