ラベル 演習 11 文字列とポインタ の投稿を表示しています。 すべての投稿を表示
ラベル 演習 11 文字列とポインタ の投稿を表示しています。 すべての投稿を表示

演習 11-10

#include <stdio.h>

int strtoi(const char *nptr){

    int num = 0, sign = 1;

    if(*nptr == '+'){
        sign = 1;
        *nptr++;
    }else if(*nptr == '-'){
        sign = -1;
        *nptr++;
    }

    while(true){
        num += *nptr - '0';
        *nptr++;
        if(*nptr == '\0') break;
        num *= 10;
    }

    return num * sign;
}

long strtol(const char *nptr){
   
    int sign = 1;
    long num = 0;

    if(*nptr == '+'){
        sign = 1;
        *nptr++;
    }else if(*nptr == '-'){
        sign = -1;
        *nptr++;
    }

    while(true){
        num += *nptr - '0';
        *nptr++;
        if(*nptr == '\0') break;
        num *= 10;
    }

    return num * sign;
}

double strtof(const char *nptr){
   
    int sign = 1;
    double num = 0;

    // 符号の処理
    if(*nptr == '+'){
        sign = 1;
        *nptr++;
    }else if(*nptr == '-'){
        sign = -1;
        *nptr++;
    }

    // 整数部の処理
    while(true){
        num += *nptr - '0';
        *nptr++;
        if(*nptr == '\0' || *nptr == '.') break;
        num *= 10;
    }

    // 小数部の処理
    if(*nptr == '.'){
        *nptr++;
        double dec = 1;
        while(true){
            num += (*nptr - '0') / (dec *= 10);
            *nptr++;
            if(*nptr == '\0') break;
        }
    }

    return num * sign;
}

int main(){

    char str1[256] = "12345";
    char str2[256] = "123.45";

    printf("str1:%s -> atoi(str1):%d\n", str1, strtoi(str1));
    printf("str1:%s -> atol(str1):%ld\n", str1, strtol(str1));
    printf("str2:%s -> atof(str2):%lf\n", str2, strtof(str2));

    return 0;
}

演習 11-9

#include <stdio.h>

int strlen(const char *s){
   
    int len = 0;

    while(*s++) len++;

    return len;
}

void strcpy(char *d, const char *s){

    while(*d++ = *s++);
}

void strncpy(char *s1, const char *s2, size_t n){
   
    char *tmp = s1;
    int len = strlen(s1);

    while(n){
        *s1++ = *s2++;
        n--;
    }

    *s1++ = '\0';

}

void strcat(char *s1, const char *s2){

    while(*s1) *s1++;
    while(*s1++ = * s2++);
}

void strncat(char *s1, const char *s2, size_t n){

    while(*s1) *s1++;
    while(n--) *s1++ = * s2++;
    *s1++ = '\0';
}

int strcmp(const char *s1, const char *s2){

    while(*s1 == *s2){
        if(*s1 == '\0') return 0;
        s1++;
        s2++;
    }
    return (unsigned char)*s1 - (unsigned char)*s2;
}

int strncmp(const char *s1, const char *s2, size_t n){
   
    while(n && *s1 && *s2){
        if(*s1 != *s2) return (unsigned char)*s1 - (unsigned char)*s2;
        s1++;
        s2++;
        n--;
    }
    if(!n) return 0;
    if(*s1) return 1;

    return -1;
}

int main(){

    char str1[256] = "abcde";
    char str2[256] = "12345";
    char str3[256] = "AaBbCcDdEe";
    int n = 3;

    printf("str1:%s\n", str1);
    printf("str2:%s\n", str2);

    printf("str1の長さは%dです\n", strlen(str1));

    printf("str2をstr1にコピーします\n");
    strcpy(str1, str2);
    printf("str1:%s\n", str1);

    printf("str2の先頭%d文字をstr1にコピーします\n", n);
    strncpy(str1, str2, n);
    printf("str1:%s\n", str1);

    printf("str2をstr1と連結します\n");
    strcat(str1, str2);
    printf("str1:%s\n", str1);

    printf("str2の先頭%d文字をstr1に連結します\n", n);
    strncat(str1, str2, n);
    printf("str1:%s\n", str1);

    printf("str2とstr1を比較します\n");
    printf("str1 : str2 ? %d\n", strcmp(str1, str2));

    printf("str2とstr1の先頭%d文字を比較します\n", n);
    printf("str1 : str2 ? %d\n", strncmp(str1, str2, n));

    return 0;
}

演習 11-8

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

void del_digit(char *s){

    int i, j;

    for(i = 0; i < strlen(s); i++){
        if(!isalpha(*(s + i))){
            for(j = i; j < strlen(s); j++){
                *(s + j) = *(s + j + 1);
            }
            i--;
        }
    }
}

int main(){

    char str[256];

    printf("文字列を入力してください:");
    scanf("%s", str);

    del_digit(str);
    printf("数字を消しました:%s\n", str);

    return 0;
}

演習 11-7

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

void str_toupper(char *s){

    int i;

    for(i = 0; i < strlen(s); i++){
        *(s + i) = toupper(*(s + i));
    }
}

void str_tolower(char *s){

    int i;

    for(i = 0; i < strlen(s); i++){
        *(s + i) = tolower(*(s + i));
    }
}

int main(){

    char str[256];

    printf("文字列を入力してください:");
    scanf("%s", str);

    str_toupper(str);
    printf("大文字:%s\n", str);

    str_tolower(str);
    printf("小文字:%s\n", str);

    return 0;
}

演習 11-6

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

char *str_chr(char *s, int c){

    int i;
    char *adress = NULL;

    for(i = 0; i < strlen(s); i++){
        if(c == *(s + i)){
            adress = (s + i);
            break;
        };
    }
    return adress;
}

int main(){

    char s[256];

    printf("入力 : ");  scanf("%s", s);

    printf("cはアドレス%pにあります\n", str_chr(s, 'c'));

    return 0;
}

演習 11-5

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

int str_chnum(const char *s, int c){

    int i, count = 0;

    for(i = 0; i < strlen(s); i++){
        if(c == *(s + i)) count++;
    }
    return count;
}

int main(){

    char s[256];

    printf("入力 : ");    scanf("%s", s);

    printf("cの個数は%d個です\n", str_chnum(s, 'c'));

    return 0;
}

演習 11-4

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

void put_string(const char *s){

    int i;

    for(i = 0; i < strlen(s); i++){
        printf("%c", *(s + i));
    }
}

int main(){

    char s[256];

    printf("入力 : ");    scanf("%s", s);

    put_string(s);

    return 0;
}

演習 11-3

#include <stdio.h>

char *str_copy(char *d, const char *s){

char *t = d;

while(*d++ = *s++);

return t;
}

int main(){

char str[128] = "ABC";
char tmp[128];

printf("str = \"%s\"\n", str);

printf("コピーするのは:"); scanf("%s", tmp);

puts("コピーしました.");
printf("str = \"%s\"\n", str_copy(str, tmp));

return 0;
}

演習 11-2

#include <stdio.h>

int main(){

int i, n = 0;
char a[][5] = {"LISP", "C", "Ada"};
char *p[] = {"PAUL", "X", "MAC"};

for(i = 0; a[i][0] != '\0'; i++){
n++;
}

for(i = 0; i < n; i++){
printf("a[%d] = \"%s\"\n", i, a[i]);
}

for(i = 0; i < n; i++){
printf("p[%d] = \"%s\"\n", i, p[i]);
}

return 0;
}

演習 11-1

#include <stdio.h>

int main(){

char *p = "123";
printf("p = \"%s\"\n", p);

//おそらく作者がしたいことはこれで
//ポインタを一つ増やすとアドレスがずれるので出力がずれる.
p = "456";
p++;

printf("p = \"%s\"\n", p);
return 0;
}