C语言字符串处理函数

2021-03-05  本文已影响0人  Dalvik_

字符串处理函数

阅读(atoi,atof,strtod,strcmp,strcmpi,strstr,strcat,tolower)

1.atoi 把字符串转换成长整型数
#include <stdio.h>
#include <stdlib.h>
int main() {
    int n;
    char *str = "12345.67";
    n = atoi(str);
    printf("string = %s integer = %d\n", str, n);
}
2.atof 把字符串转换成浮点数
#include <stdio.h>
#include <stdlib.h>
int main() {
    float n;
    char *str = "12345.67";
    n = atof(str);
    printf("string = %s integer = %f\n", str, n);
}
3 strtod 将字符串转换为double型值
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char input[80], *endptr;
    double value;

    printf("Enter a floating point number:");
    gets(input);
    value = strtod(input, &endptr);
    printf("The string is %s the number is %lf\n", input, value);
    return 0;
}
4.strcmp 字符串比较
#include <string.h> 
#include <stdio.h> 

int main(void) 
 { 
    char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc"; 
    int ptr; 

    ptr = strcmp(buf2, buf1); 
    if (ptr > 0) 
       printf("buffer 2 is greater than buffer 1\n"); 
    else 
       printf("buffer 2 is less than buffer 1\n"); 

    ptr = strcmp(buf2, buf3); 
    if (ptr > 0) 
       printf("buffer 2 is greater than buffer 3\n"); 
    else 
       printf("buffer 2 is less than buffer 3\n"); 

    return 0; 
 } 
4 strcmpi 不区分大小写比较字符串
#include <string.h>
#include <stdio.h>

int main(void)
{
    char *buf1 = "bbb", *buf2 = "BBB", *buf3 = "BbB";
    int ptr;

    ptr = stricmp(buf2, buf1);
    if (ptr > 0)
        printf("buffer 2 is greater than buffer 1\n");
    else if (ptr == 0)
        printf("buffer 2 equals buffer 1\n");
    else
        printf("buffer 2 is less than buffer 1\n");

    ptr = stricmp(buf2, buf3);
    if (ptr > 0)
        printf("buffer 2 is greater than buffer 3\n");
    else if (ptr == 0)
        printf("buffer 2 equals buffer 3\n");
    else
        printf("buffer 2 is less than buffer 3\n");


    return 0;
}
5. strstr 根据字符串截取指定字符串第一次出现的地方
#include <stdio.h>
#include <string.h>

int main(void)
{
    char *str1 = "Borland International", *str2 = "Inter", *ptr;

    ptr = strstr(str1, str2);
    printf("The substring is: %s\n", ptr);
    return 0;
}
输出: The substring is: International
6 strcat 字符串拼接
#include <string.h>
#include <stdio.h>

int main(void)
{
    char destination[25];
    char *blank = " ", *c = "C++", *Borland = "Borland";

    strcpy(destination, Borland);
    strcat(destination, blank);
    strcat(destination, c);
    printf("%s\n", destination);
    return 0;
}
7 tolower 把字符转换成小写字母
#include <string.h>
#include <stdio.h>
#include <ctype.h>

int main(void) {
    int length, i;
    char *string = "THIS IS A STRING";
    length = strlen(string);
    char *dest;
    for (i = 0; i < length; i++) {
        dest[i] = tolower(string[i]);
    }
    *(dest+length)='\0';
    printf("%s\n", dest);
    return 0;
}

作业 字符串截取函数

void subString(char *string, char *result, int start, int end) {
    int length;
    char *temp = string;
    length = strlen(temp);
    if (start >= 0 && end <= length) {
        for (int i = start; i < end; i++) {
            result[i - start] = temp[i];
        }
        result[end - start] = '\0';
        printf("输入结果%s\n", result);
    } else {
        printf("输出结果%s\n", "下标越界");
    }
上一篇 下一篇

猜你喜欢

热点阅读