Thread: converting two strings to upper and lower case using a function

  1. #1
    Registered User
    Join Date
    Feb 2014
    Posts
    27

    converting two strings to upper and lower case using a function

    Hi all , hope you're well

    converting two strings to upper and lower case using a function-capture221-jpg

    My code so far

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    
    void uplostr(char *s1, char *s2)
    
    
    
    
    int main()
    {
      char  string1[] = "My NaMe Is SJaD";
      char  string2[] = "My NaMe Is SJaD";
    
    
      printf(" Uppercase : %s \n Lowercase : %s\n\n",uplostr(string1),uplostr(string2));
    
    
    
    
      return 0;
    }
    
    
    void uplostr(char *s1, char *s2)
    {
        char  *p;
        char  *p2;
    
    
    
    
      for (*p=s1; *p != '\0'; p++)
      {
        *p=toupper(*p);
      }
    
    
      for (*p2=s2; *p2 != '\0'; p2++)
      {
        *p2 = tolower(*p2);
      }
       return;
    }
    Code:
    I keep getting these 3 errors and I have now idea how to deal with them :
    error expected '=', ',', ';', 'asm' or '__attribute__' before '{' token|.
    error  expected '=', ',', ';', 'asm' or '__attribute__' before '{' token|.
    error expected '{' at end of input|.
    
    any hits would be appreciated :)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,996
    You're missing the terminating semi-colon on this line:
    Code:
    void uplostr(char *s1, char *s2)
    You're calling uplostr wrongly here:
    Code:
    printf(" Uppercase : %s \n Lowercase : %s\n\n",uplostr(string1),uplostr(string2));
    I would expect:
    Code:
    uplostr(string1, string2);
    printf(" Uppercase : %s \n Lowercase : %s\n\n", string1, string2);
    In the implementation of uplostr, you don't actually need p and p2 because you can use s1 and s2 directly. If you do want to use p and p2, then you need to be careful how you set the value of the pointer (not just what the pointer points to).
    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

  3. #3
    Registered User
    Join Date
    Feb 2014
    Posts
    27
    thanks for the repley laserlight

    I took your advice and change the code accordingly but the out put prints the string the same as it as without changing the case
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    
    void uplostr(char *s1, char *s2);
    
    
    
    
    int main()
    {
      char  string1[] = "My NaMe Is SJaD";
      char  string2[] = "My NaMe Is SJaD";
        uplostr(string1,string2);
      printf(" Uppercase : %s \n Lowercase : %s\n\n",string1,string2);
    
    
    
    
      return 0;
    }
    
    
    void uplostr(char *s1, char *s2)
    {
        int i,j;
    
    
      for (i=0; i != '\0'; i++)
      {
        *s1=toupper(*(s1+i));
      }
    
    
      for (j=0; j != '\0'; j++)
      {
        *s2 = tolower(*(s2+i));
      }
       return;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,996
    *s1 is equivalent to s1[0]. In other words, you're only changing the first element on every iteration of the array. Rather, make use of s1 in the same way as you did with p in your previous attempt. You do not need those array indices since you are going to use pointer notation only.

    If you want to use *(s1+i), then you might as well use s1[i] because it is likely to be easier to understand.
    Last edited by laserlight; 12-06-2014 at 01:45 PM.
    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
    Feb 2014
    Posts
    27
    I understand now, thanks
    I followed your advice and the code works perfect


    you're a life saver laser <3

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. Simple program problem from upper to lower case
    By steals10304 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2009, 02:31 AM
  3. help...Lower and Upper case problem only
    By j4k50n in forum C Programming
    Replies: 9
    Last Post: 04-19-2007, 12:39 AM
  4. upper case to lower case problem
    By Jasonymk in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 05:35 AM
  5. string converting upper/lower
    By jlamn in forum C Programming
    Replies: 9
    Last Post: 09-24-2002, 06:01 PM