字符串处理函数和函数

文件流     输入:stdin   输出:stdout
字符串处理函数:
1.gets和puts:
gets 从标准设备上获取一个字符串  这个字符串可以带空格  直到接收到换行字符或者字符串结束标志才停止    gets是一种不安全的
puts从标准设备上输出一个字符串  这个字符串自带换行功能 
gets(str)与scanf("%s",str)区别:
gets(str)允许输入的字符串含有空格
scanf("%s",str)不允许有空格
注意:由于scanf()和gets()无法知道字符串s的大小,必须遇到换行符或读到文件结尾为止才停止输入,因此容易导致字符数组越界(缓冲区溢出)的情况
 
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main()
{
 char ch[100];
 //char * p = gets(ch);
 //获取一个字符
 scanf("%[^\n]", ch);//输出一个字符  并换行
 int value=puts(ch);
 //printf("%s", ch);
 //printf("%s", p);
 printf("%d", value);
 system("pause");
 return EXIT_SUCCESS;
}

2.fgets和fputs
fgets(字符指针,大小,输入流)
如果输入的大小小于原始的指针对应区域的大小,会在字符串输入完成后自动加上\n\0
fgets是安全的  会接受固定大小的字符串
如果接受少于大小  会将\n加入到字符串中
如果接受大于等于的大小  会将\n加入到字符串中   始终会将\0加入到字符串中
fgets(字符指针,大小,输入流)
如果输入的大小小于等于原指针对应的区域大小,会自动加上\0不会加上\n
屏蔽数据类型     %*d屏蔽数字   %*c屏蔽字符
屏蔽一个区间之内的字符串
strlen  需要导入#include  <string.h>
char arr[] = "hello";
 int len=strlen(arr);
 printf("字符串的有效长度%d\n", len);
 printf("字符串的长度%d\n", sizeof(arr));
strlen  忽略\0之后的字符        不能处理字符数组

strcpy    需要导入#include  <string.h>
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main()
{
 char arr1[] = "hello world";
 char arr2[100];
 //参数:目标字符串,源字符串
 strcpy(arr2, arr1);
 printf("%s\n", arr2);
  system("pause");
 return EXIT_SUCCESS;
}
strncpy//参数:目标字符串   源字符串   字符长度     注意:有限拷贝不会将\0拷贝到目标字符串中
在后面写上          目标字符串=0         即可
有选择个数的拷贝字符

strcat      需要导入#include  <string.h>
将源字符串追加到目标字符串结尾,‘\0’也会追加过去
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main()
{
 char arr1[100] = "hello";
 char arr2[] = "world";
 strcat(arr1, " ");//“字符串(空格也行)”
 strcat(arr1, arr2);
 printf("%s\n", arr1);
 printf("%s\n", arr2);
  system("pause");
 return EXIT_SUCCESS;
}

strncat
选择性追加
注意:有限追加也会将\0追加

strcmp     需要导入#include  <string.h>
比较两者的大小    (比较的是字符ascii码大小)  比较 \0之前的所有有效字符
strncmp 有限比较
 

sprintf   需要导入#include <stdio.h>
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main()
{
 char arr[] = "hello";
 int a = 123;
 char dest[100];
 //将数据格式化后放入到字符串中
 sprintf(dest, "%s %d", arr, a);
 printf("%s\n", dest);
  system("pause");
 return EXIT_SUCCESS;
}
sscanf      从指定地方读取数据

strchr   需要导入#include  <string.h>
在字符串中查找字符位置
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main()
{
 char arr[] = "hello";
 char ch = 'l';
 char * p=strchr(arr, ch);
 printf("%s\n", p);

  system("pause");
 return EXIT_SUCCESS;
}
strstr      查找后面的字符在前面的字符串的位置
strtok     需要导入#include  <string.h>
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main()
{
 char arr[] = "www.itcast.cn";
 char * p = strtok(arr, ".");
 //将切割点用\0表示
 while (p != NULL)
 {
  printf("%s\n", p);
  p = strtok(NULL, ".");
 }
 //printf("%s\n", p);
  system("pause");
 return EXIT_SUCCESS;
}

atoi 转为整型                 需要导入#include <stdlib.h>
atof  转为双精度浮点型
atol    转为长整型
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
int main()
{
 char arr[] = "      -3.141592653";
 //char arr[] = "100";
 //int a = atoi(arr);
 //long a = atol(arr);
 float a = atof(arr);
 //printf("%d\n", a);
 printf("%f\n", a);
  system("pause");
 return EXIT_SUCCESS;
}
 
函数:
 函数名(参数列表)
{
       函数体
}
函数的返回值类型        函数名称  (参数列表)
{
           函数体
           return   返回值
}
如果不需要返回值就用void
例如:
void bubble(int arr[],int len)
{
      for.......
      return;(不写也行)
}
实参会在函数执行后释放掉
函数定义中的参数列表中的数据称为函数的形式参数     形参
函数调用中的参数列表中的数据称为函数的实际参数     实参
形参接受实参   在函数内部运算
函数独立的在外边写,不写主函数内
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
//1.声明   声明函数的格式
extern   int    add(int a, int b);
//声明格式:extern   返回值类型  函数名称  参数
int main()
{    //3.调用    执行函数完成某项功能    声明定义一次之后可以多次调用
 int a = add(10, 20);
 printf("%d\n", a);
  system("pause");
 return EXIT_SUCCESS;
}
//2.定义     函数的定义就是对函数功能的实现
int add(int a, int b)
{
 return a + b;
}
实参会影响形参的值,形参不会影响实参的值
形参不能赋值
在定义函数时指定的形参,必须是,类型+变量的形式
实参出现在主调函数中

程序中无论遇到那个return都代表函数的结束
exit(0)代表程序的结束;
 
多文件联合编程:
在文件中函数是全局函数
头文件的作用:1.函数,变量的声明
                         2.系统库的调用
        
#pragma once     头文件只包含一次
或者
#ifndef__SOMEFILE_H__
#define__SOMEFILE_H__
#endif

猜你喜欢

转载自www.cnblogs.com/mingstatebird/p/10707445.html