2

I'm still a beginner at making function and C programming. I'm trying to make a function for turning it into uppercase, but it seems I messed it up at the pointer(?)

#include <stdio.h>

void mytoupper(char *s[]) {
    int i = 0;
    while (s[i] != '\0') {
        if (s[i] >= 'a' && s[i] <= 'z') {
            s[i] = s[i] - 32;
        }
        i++;
    }
    return s;
}

int main(void) {
    char s[32];
    printf("Insert string:");
    printf("%s", s);
    printf("%s", mytoupper(s[32]));
    return 0;
}
6
  • You probably wanted to scanf instead of printf("%s", s); Commented Jun 27, 2018 at 20:46
  • 1
    Typo in the code: printf("%s", s); to scanf("%s", s); regardless of other deficiencies. Commented Jun 27, 2018 at 20:47
  • mytoupper(s[32]) -> mytoupper(s) Commented Jun 27, 2018 at 20:47
  • void mytoupper(char * s[]) needs to be either void mytoupper(char *s) or void mytoupper(char s[]). Commented Jun 27, 2018 at 20:49
  • ... and it should not return anything. Breaking the printf("%s", mytoupper(s[32])); line second time. Commented Jun 27, 2018 at 20:50

2 Answers 2

3

There are multiple problems in your code:

  • the definition for mytoupper is incorrect: it should take a char *s argument instead of char *s[], and return char *.
  • Changing the character from lower to upper case should not use a hard coded value of 32 that only works for ASCII, use a more generic approach with s[i] = s[i] - 'a' + 'A';
  • To read the string, use scanf("%31s", s); instead of printf("%s", s); and it is highly recommended to test the return value of scanf()
  • The argument in printf("%s", mytoupper(s[32])); is incorrect: you should just write printf("%s", mytoupper(s));

Here is a corrected version:

#include <stdio.h>

char *mytoupper(char *s) {
    int i = 0;
    while (s[i] != '\0') {
        if (s[i] >= 'a' && s[i] <= 'z') {
            s[i] = s[i] - 'a' + 'A';
        }
        i++;
    }
    return s;
}

int main(void) {
    char s[32];
    printf("Insert string:");
    if (scanf("%31s", s) == 1) {
        printf("%s\n", mytoupper(s));
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. When i space it, it only uppercase the first word. To prevent it, should i use fgets?
scanf with %s reads just one word. If you wish to process a whole line, use fgets with a larger buffer. If you wish to process the whole standard input stream contents, use a while loop, testing the return value of fgets(): while (fgets(s, sizeof s, stdin)) { fputs(mytoupper(s), stdout); }
0

There are a few spots that will not work as expected in your code.

  1. You are not properly getting user input, instead you are trying to print an 'empty' string.

    printf("%s",s);
    

    You could change this to:

    scanf("%31s",s);
    
  2. You are accepting an array of char pointers in your function mytoupper, but this is not needed. You can instead simply pass it an array of chars.

    void mytoupper(char s[]) 
    
  3. You are attempting to use a return value of a void function. You can either call the function then print the string, or have the function return the string.

    If you want to change the string then print it seperately you will need to change mytoupper to no longer return anything and keep it as a void return type.

    mytoupper(s);
    printf("%s", s);
    

    Or change the function to:

    char* mytoupper(char s[]) {
    

    Then print the string:

    printf("%s", mytoupper(s));
    

3 Comments

scanf("%s",&s); passes the wrong type. Drop the & and recommend to employ a width limit. scanf("%31s",s); - or even better, use fgets().
scanf("%s",&s); is wrong. It might "work" if s is defined locally, but if s is a function argument, there is a big difference between s and &s.
char *s[] is not a pointer to an array, it's an array of pointers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.