Skip to content

Instantly share code, notes, and snippets.

@rmccullagh
Created October 22, 2014 22:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rmccullagh/5cf69c7c50be9faf87a5 to your computer and use it in GitHub Desktop.
Save rmccullagh/5cf69c7c50be9faf87a5 to your computer and use it in GitHub Desktop.
#include <stdio.h> // fprintf
#include <stdlib.h> // malloc
#include <string.h> // strlen
#include <ctype.h> // toupper, tolower
/*
* a string is an array of characters, in C, all arrays
* are always passed never passed value, always a pointer to
* the variable
* This is why the caller does not need to call the function like:
* camel_case_rev(src, &dest, len)
*
* Since here the array of characters ("a string") is already being
* passed as a pointer value
*/
void str_case_rev(const char *src, char *dest, size_t len)
{
size_t i = 0;
for(; i < len; i++)
{
if(src[i] <= 'Z' && src[i] >= 'A')
{
*(dest + i) = (char)tolower((int)src[i]);
}
else if(src[i] <= 'z' && src[i] >= 'a')
{
*(dest + i) = (char)toupper((int)src[i]);
}
else
{
*(dest + i) = src[i];
}
}
i++;
*(dest + i) = '\0';
}
int main(int argc, char **argv)
{
if(argc < 2)
{
fprintf(stderr, "Usage: %s <string>\n", argv[0]);
return EXIT_FAILURE;
}
char *dest = NULL;
size_t len = strlen(argv[1]);
dest = malloc(len + 1);
if(NULL == dest)
{
fprintf(stderr, "Memory error\n");
return EXIT_FAILURE;
}
str_case_rev(argv[1], dest, len);
fprintf(stdout, "%s\n", dest);
free(dest);
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment