0

Its a code to convert lowercase chars in a vector to uppercase chars. Im getting error in lines 21,23,24,26,29. Im newbie programing in C. Please help me.

int main()
{
    char orig[30];
    char dest[30];

    printf("Write a string :");
      scanf("%s",orig);

      char uppercase(dest,orig);
       printf("uppercase string = ",dest);


}



char uppercase(char destination,char origin){

    int i = 0;

      while(origin[i]!='\0'){

         if(origin[i]>='a'&& origin[i]<='z'){
            destination[i]= origin[i] +'A'-'a';
         }
         else destination[i]=origin[i];
                i++;
      }
    destination[i]='\0';

return destination;

}
Fran
  • 1
  • 1

2 Answers2

0

Okay, so you might have a little mess there ^^

While the general idea is right, your method call doesn't work that way.

Try something like this

int main()
{
    char orig[30];
    char dest[30];

    printf("Write a string :");
    scanf("%s",orig);

    dest = uppercase(orig);
    printf("uppercase string = ",dest);


}



char[] uppercase(char origin){
    char destination[30];
    int i = 0;

      while(origin[i]!='\0'){

         if(origin[i]>='a'&& origin[i]<='z'){
            destination[i]= origin[i] +'A'-'a';
         }
         else destination[i]=origin[i];
                i++;
      }
    destination[i]='\0';

return destination;

}

though there might be even more Problems, but I am not sure which language you are programming in, so I can't really help you with that.

DeastinY
  • 341
  • 1
  • 2
  • 11
0

There are a couple of problems here:

  1. You don't declare your uppercase function before you use it. While old C standards allow implicit function declarations, this doesn't work the way you want it to here. For more info see: Implicit function declarations in C
  2. You have type mismatches in your function signature. You should pass a char array either as char * or as char [], not as plain char. Same for the return type.
  3. Not really an error, but it's good practice to use fgets instead of scanf to input strings, as this way you can protect against buffer overflow problems. See this too: https://stackoverflow.com/a/1248017/2327880

This should work:

#include <stdio.h>
#include <string.h>

// uppercase function declaration
char * uppercase(char *, char *);

int main() {

    char orig[31];
    char dest[31];

    printf("Write a string : ");

    // use fgets instead of scanf in order
    // to prevent buffer overflow problems
    fgets(orig, 31, stdin);

    // remove trailing newline
    orig[strlen(orig) - 1] = '\0';

    uppercase(dest, orig);
    printf("uppercase string = %s", dest);

    return 0;
}

// uppercase function definition
char * uppercase(char * destination, char * origin) {

    int i = 0;

    while(origin[i] != '\0') {

        if(origin[i] >= 'a' && origin[i] <= 'z') {
            destination[i] = origin[i] + 'A' - 'a';
        } else {
            destination[i] = origin[i];
        }

        i ++;
    }

    destination[i] = '\0';

    return destination;
}
Community
  • 1
  • 1
d0c
  • 515
  • 5
  • 7