Skip to content

Instantly share code, notes, and snippets.

@y0t4
Last active August 29, 2015 14:03
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 y0t4/e37ab3ecb8143003b3d5 to your computer and use it in GitHub Desktop.
Save y0t4/e37ab3ecb8143003b3d5 to your computer and use it in GitHub Desktop.
てきとーに
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
* 文字の大小変換
* 記号は何もしない
*/
char convert_upper_lower(char buff)
{
int diff_upper_lower = 'a'-'A';
if ( 'A'<=buff && buff<='Z' ) {
return buff + diff_upper_lower;
}
if ( 'a'<=buff && buff<='z' ) {
return buff - diff_upper_lower;
}
return buff;
}
void replace(char *dest, char *str)
{
/* strの文字を1文字ずつconvert_upper_lowerする */
for (int i = 0; str[i] != '\0'; i++) {
dest[i] = convert_upper_lower(str[i]);
}
}
void print_data(char *dest, char *str)
{
printf("str : %s\n", str);
printf("dest: %s\n", dest);
}
int main(int argc, char *argv[])
{
/* パラメータが無い場合はエラー出力 */
if (argc == 1) {
fprintf(stderr, "Usage: ./a.out [string] [string] ...\n");
exit(1);
}
char *str; /* インプット */
char *dest; /* アウトプット */
int length = 0;
/*
* パラメータ全部の文字数を取得
* パラメータ間の' '、文字列の最後の'\0'も含む
*/
for (int i=1; i < argc; i++) {
length += strlen(argv[i])+1;
}
/* length分のメモリを確保 */
str = (char *)malloc(length);
dest = (char *)malloc(length);
/*
* パラメータを一つの文字列に結合
* example:
* "This","is","a","pen"
* -> "This is a pen"
* */
for (int i=1; i < argc; i++) {
int word_len = strlen(argv[i]);
strncat(str, argv[i], word_len);
strcat(str, " ");
}
str[length] = '\0'; /* 文字列の最後の' 'を'\0'にする */
replace(dest, str);
print_data(dest, str);
/* 確保したメモリの解放 */
free(str);
free(dest);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment