-2

I have written a working program but the form is incorrect for the task. Can somebody please help me change the code to

"void lower(char* s)"

Tried to change and position it but no working outcome. Doesnt fully understand the void type and one function in other and so on.

Code:

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

int main ()
{
  int i=0;
  const char s[]="SoMeThing SOmEThing stRing\n";
  char lower;
  printf("%s",s);
  while (s[i])
  {
    lower=s[i];
    putchar(tolower(lower));
    i++;
  }
  return 0;
}

Thank you

alk
  • 69,737
  • 10
  • 105
  • 255
realmean
  • 7
  • 1
  • 3
    What is not working in your program? What is its output, and what output you expect to get? – Teo Zec May 29 '14 at 11:57
  • 1
    `I have written a working program but the form is incorrect for the task.` Then let us know what your task is. – starrify May 29 '14 at 12:01
  • Do you mean to put the code inside a function "void lower(char *s)" and not in main()? – Morad May 29 '14 at 12:04
  • Everything works but I got an bunch of tasks from my lecturer. The full task is: "Implement function void lower(char* s) which would convert all upper case letters to the lower case." The main code which converts the symbols tolower must be in "void lower(char* s) bracket and only print in main. I dont know how to change/position the code so the outlook is that. – realmean May 29 '14 at 12:05
  • Are you familiar with methods/functions? – Tim May 29 '14 at 12:06
  • *Doesnt fully understand the void type and one function in other and so on.* in C you cannot have nested functions – Tim May 29 '14 at 12:10
  • I understand that they perform an individual tasks but in this case, I got stuck with printf, where it is only one but has two outcomes. The const string and changed string. – realmean May 29 '14 at 12:12
  • _"Implement function void lower(char* s) which would convert all upper case letters to the lower case."_ : `const char s[]="SoMeThing SOmEThing stRing\n";` isn't possible to change.(because it's `const char array`) **it means the only display?** – BLUEPIXY May 29 '14 at 12:29

3 Answers3

1
#include <stdio.h>
#include <ctype.h>

void lower(char *s){
    for(;*s;++s)
        *s=tolower(*s);
}

int main (){
    char s[]="SoMeThing SOmEThing stRing\n";

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

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • Yes, Thank you all for answers. Now I get the picture :) The unknown part was the lower(s) code for applying void function in main. – realmean May 29 '14 at 12:39
  • That's right, just call it simply. Function is performed as a side effect. – BLUEPIXY May 29 '14 at 12:42
0

I am not sure this is what you are looking for, but it might be.

From your title and the highlighted portion, I am assuming that you want to get rid of

int main() {}

and title it

void lower(char *s) {}

While you can easily do that with a copy paste, understand that this file can not be compiled on its own anymore. The C compiler will look for a

[return type] main() {}

to be the starting point for the program. With out this, there is no program.

I believe you are wanting to make a library and use the lower function in another program. To do this, I would create a .h file with the includes and function name:

void lower(char *s);

This can replace all of your other includes with a single include of this file:

#include "file.h"

You can use the above line in other programs to make this function available in these programs. If you have multiple functions in a library file and you do not want them all to be accessible outside of the file, ensure you use the 'static' keyword in front of methods you do not want available:

static int otherFunction();

Let us know if this is what you are looking for answer wise. If not, please try to clarify your request.

houckrj
  • 118
  • 1
  • 12
0

This function should work.

#include <string.h>

/* The function changes every upper-case letter of string s
   to its corrispondent lower-case. */
void lower(char *s)
{
    /* Iterates through string */
    for (int i = 0; i != strlen(s); i++) {
        s[i] = tolower(s[i]);
    }
}

The void type means that the function doesn't return any value: it just does some work and terminates, but it doesn't return anything to the caller.

Note that this function will change the characters of the string s instead of just printing them.

Teo Zec
  • 383
  • 2
  • 11