26、字符串的操作

字符串的操作

一、字符串的反向输出(逆置)

/*
	2017年3月17日13:18:39
	功能:将字符串反向输出
*/
#include"stdio.h"
int main ()
{
	char a[100];
	char b[100];
	printf("please input one array: ");
	gets(a);
	char *pa = a;
	char *ppa = a;
	char *pb = b;
	while (*pa)
	{
		pa++;
	}
	while(*ppa)
	{
		pa--;
		*pb = *pa;
		pb++;
		ppa++;
	}
	*pb = '\0';

	puts(b);

	return 0;
}
/*
	总结:
	在VC++6.0中显示的结果:
	——————————————————————
	please input one array: abcdefgh
	hgfedcba
	——————————————————————

*/

 

二、找字符串1在字符串2中首次出现的位置

/*
	2017年3月16日21:10:38
	功能:找字符串1在字符串2中首次出现的位置
*/
#include"stdio.h"
int main()
{
	int i, j, flage, m = -1;
	char str1[100];
	char str2[100];
	printf("please  input a long string: \n");
	gets(str1);
	printf("please input a short string: \n");
	gets(str2);
	
	for(i = 0; str1[i] != '\0'; i++ )
	{
		for( j = 0; str1[i+j] != '\0' && str2[j] != '\0'; j++)
		{
			if(str1[i+j] != str2[j])
				break;
		}
		if (str2[j] == '\0')
		{
			flage = 1;												//此处设置的标志位很重要,对于后续以何种方式输出做一个判断
			break;
		}
	}
	if (flage == 1)
		printf("the result is %d\n", i);
	else
		printf("the result is %d\n", m);

	return 0;
}
/*
	总结:
	在VC++6.0中显示的结果:
	————————————————————————
	存在的情况
	please  input a long string:
	this is very good
	please input a short string:
	very
	the result is 8
	不存在的情况
    please  input a long string:
	asdasdfg
	please input a short string:
	ab
	the result is -1
	————————————————————————
*/

 

三、找字符串中的元素

/*
	2017年7月1日14:39:59
	功能:找字符串中的元素
*/
#include"stdio.h"
int main()
{
	char str[100];
	char ch;
	int flage;
	printf("please input a string : ");
	gets(str);
	printf("please input a character : ");
	ch = getchar();
	char *pstr = str;

	while(*pstr)
	{
		if(*pstr != ch)					
		     pstr++;
		else
		{   
			flage = 1;
			break;
		}
	}

	if(flage == 1)
		printf("the character %c is the character of the string %s\n ",ch, str);
	else 
		printf("the character %c is not the character of the string %s\n ",ch, str);
		if(*pstr)					
		;
		else 
		{
			*pstr = ch;
		}
		*(++pstr)= '\0';
		puts(str);

	return 0;
}
/*
在VC++6.0中显示的结果:
————————————————————————————————
please input a string : asdfghj
please input a character : a
the character a is the character of the string asdfghj
 asdfghj

please input a string : asdfgh
please input a character : m
the character m is not the character of the string asdfgh
 asdfghm
————————————————————————————————
*/

 

四、统计字符串中指定子字符串的个数

/*
	2017年3月13日17:02:55
	功能:统计字符串中指定子字符串的个数
*/
#include"stdio.h"

int totsubstmum(char *str, char *substr);

int main()
{
	char str[100];
	char substr[100];

	printf("please input a string: ");
	gets(str);
	printf("please input its substring: ");
	gets(substr);

	printf("%d times\n",totsubstmum(str,substr));

	return 0;

}

int totsubstmum(char *str, char *substr)
{
	int count = 0;															//计数变量

	for ( int i = 0; str[i] != '\0'; i++)									//外循环是遍历长字符串中所有字符元素																	
	{
		for (int j = 0; str[i+j] != '\0' && substr[j] != '\0'; j++)			//内循环是遍历短字符串依次比较字符串中的字符元素是否相等
		{
			if(str[i+j] != substr[j])										//如果不相等跳出内循环,遍历外循环的下一个字符元素,
				break;														//如果相等则比较长字符串在内循环中的下一个字符元素和短字符串的下一个字符元素是否相等
		}

		if (substr[j] == '\0')												//跳出外循环的有多种情况,1、两个字符元素没有相同的,2、短字符串中没有了可以继续比较的字符元素
			count++;
	}
	return count;
}
/*
	总结:
	在VC++6.0中显示的结果:
	————————————————————————
	please input a string: weareisisisisisgood
	please input its substring: is
	5 times
	————————————————————————
*/

 

五、删除字符串中指定的字符  

/*
	2017年3月17日13:34:09
	功能:删除字符串中指定的字符
*/
#include "stdio.h"
int main ()
{
	char str1[100];
	char str2[100];
	char ch;
	printf("please input one string: ");
	gets(str1);
	printf("please input a character: ");
	scanf("%c",&ch);
	char *pstr1 = str1;
	char *pstr2 = str2;
	while (*pstr1)
	{
		if(*pstr1 != ch)
		{
			*pstr2 = *pstr1;
			pstr2++;
		}
		pstr1++;
	}
	*pstr2 = '\0';

	puts(str2);

	return 0;
}
/*
在VC++6.0中显示的结果:
————————————————————————
	please input one string: asasasasasasa
	please input a character: a
	ssssss
————————————————————————
*/

 

六、对字符串下标为奇数的字符按ASCII从大到小排序  

/*
	2017年3月17日08:56:32
	功能:对字符串下标为奇数的字符按ASCII从大到小排序
*/
#include"stdio.h"
int main ()
{
	int i, j, k, n = 0, t;
	char a[100];
	char b[100];
	char c[100];
	printf("please input a string: ");
	gets(a);
	char *pa = a;
	char *pb = b;
	char *pc = c;
	while(*pa)
	{
		pa++;
		n++;
	}
	for(i = 0, j = 0, k = 0; i < n; i++)
		if( i % 2 != 0)
			b[j++] = a[i];
		else
			c[k++] = a[i];
		b[j] = '\0';
		c[k] = '\0';
	for(int x = 0; x < j; x++)
	{
		for(int y = x; y < j; y++ )
		{
			if(b[y] >= b[x])
			{
				t = b[y];
				b[y] = b[x];
				b[x] = t;
			}
		}
	}
	for(i = 0; i < n; i++)
	{
		if(i % 2 == 0)
		{	
			a[i] = *pc;
			pc++;
		}
		else
		{	
			a[i] = *pb;
			pb++;
		}
	}
	puts(a);


	return 0;
}
/*
	总结:
	在VC++6.0中显示的结果:
	————————————————————————
	please input a string: abcdefgh
	ahcfedgb
	————————————————————————
*/

 

七、将字符串2链接到字符串1的尾部

/*
	2017年3月17日13:10:55
	功能:将字符串2链接到字符串1的尾部
*/
#include "stdio.h"
int main()
{
	char str1[100];
	char str2[100];
	printf("please input a string1: ");
	gets(str1);
	printf("please input a string2: ");
	gets(str2);
	char *pstr1 = str1;
	char *pstr2 = str2;

	while(*pstr1)
	{
		pstr1++;
	}
	while(*pstr2)
	{
		*pstr1 = *pstr2;
		pstr1++;
		pstr2++;
	}
	*pstr1 = '\0';

	puts(str1);

	return 0;
}
/*
	总结:
	在VC++6.0中显示的结果:
	——————————————————————————————
	please input a string1: asdfghk
	please input a string2: 12345
	asdfghk12345
	——————————————————————————————
*/ 

 

八、将字符串中的内容逆置

/*
	2017年7月1日18:01:26
	将字符串中的内容逆置
*/
#include"stdio.h"
int main ()
{
	int i, m, n = 0, t;
	char str[100];
	printf("please input a string : ");
	gets(str);
	char *pstr = str;
	while(*pstr)
	{
		n++;
		pstr++;
	}
	m = n -1;
	for(i = 0; i < m; i++,m--)
	{
		t = str[i];
		str[i] = str[m];
		str[m] = t;
	}

	puts(str);

	return 0;
}

  

九、输出字符串中指定元素的个数

/*
	2017年7月1日18:02:33
	输出字符串中指定元素的个数
*/
#include"stdio.h"
int main()
{
	int n = 0, m = 0;
	char str[100], b[100];
	char ch;
	printf("please input a string : ");
	gets(str);
	printf("please input a character : ");
	ch = getchar();
	char *pstr = str;
	while(*pstr)
	{
		pstr++;
		m++;
	}
	for(int i = 0, j = 0; i < m; i++)
	{
		if(str[i] == ch)
		{
			b[j++] = i;
			n++;
		}
	}
	for(int k = 0; k < j; k++)
	{
		printf("%d  ", b[k]);
	}
	printf("\n");
	printf("%d\n", n);

}

 

十、字符串所有的字符左移n位,串中前n个字符移到最后

/*
    2017年3月13日14:12:45
    功能:把字符串所有的字符左移n位,串中前n个字符移到最后
*/
#include "stdio.h"
int main (void)
{
    char a[100];                                         //定义两个字符串空间
    char b[100];
    int m, n;                                            //定义两个变量,n代表将移动几个元素
    printf("please input a string: ");
    gets(a);                                             //此处获取的是字符串
    char *pa = a;                                        //获取a数组的首地址,只有将数组名定义成一个指针才能进行运算操作
    char *pb = b;                                        //获取b数组的首地址
    char *ppb = b;                                       //因为指针参与运算后指针会后移,但是衔接需要从字符串的第一个元素开始,此操作就是从新将指移到首部
    printf("please input a number: ");
    scanf("%d",&n);
    for(int j = 0;j < n ;j++)                            //将需要放入到a数组后面的元素放在一个新的数组b中
    {
        *pb = a[j];                                       //此部分操作只是将元素放入到b数组中,但是它们还不是字符串
        pb++;
    }
    *pb = '\0';                                           //因为b数组只是开辟了空间,但是若要存入字符串必须加入结尾标识符'\0'
    while (*pa)
    {
        *pa = *(pa+n);                                    //将第n个元素放到第一个位置
        pa++;                                             //因为直至遇到结尾标识符,while()循环才会跳出来,此时指针在末尾
    }
    m = n;                                                //此操作的目的将指针移到需要插入的位置,有几个元素就需要向前移动几次
    while (m--)
    {
        pa--;
    }
    while(*ppb)                                            //直至b数组中所有元素插入完,就结束循环,
    {
        *pa = *ppb;
        pa++;
        ppb++;
    }
    puts(a);

    return 0;
}
/*
    总结:
    在VC++6.0中显示的结果:
    ——————————————————————
    please input a string: 12345678
    please input a number: 3
    45678123
    ——————————————————————
*/
 

 

  

十一、判断字符串是否回文

 方法一:

/*
    2017年3月13日10:57:54
    功能:判断字符串是否回文
*/
#include "stdio.h"
int main (void)
{
    int flage;
    char a[100];
    printf ("please input a string : ");
    gets(a);
    char *pa = a;
    char *ppa = a;
    while (*ppa)
    {
        ppa++;
    }
    ppa--;
    while(*pa)
    {
        if(*pa == *ppa )
        {
            pa++;
            ppa--;
        }
        else
            flage = 0;
            break;

    }

    if(flage == 0)
        printf("This is not Huiwen\n");
    else 
        printf("This is Huiwen\n");

    return 0;
}
/*
    总结:
    在VC++6.0中显示的结果:
    ——————————————————
    please input a string : level
    This is Huiwen

    please input a string : levved
    This is not Huiwen
    ——————————————————
*/
 

 方法二:

/*
    2017年3月13日23:48:51
    功能:判断字符串是否回文
*/
#include"stdio.h"
#include"string"
void fun(char *);                                     //函数声明的类型与调用的类型一直,因为没有需要返回到主函数的参数,故采用空类型
int main()                                            //函数入口
{
    char a[100];                                      //定义了一个字符数组,并将其分配了100个内存空间
    printf("please input a string :");
    gets(a);                                          //获取字符串(此时的字符串在末尾添加了结尾标识符),注意与整型数组相区别
    fun(a);                                            //fun()函数中的a是char*类型,即数组名相当一个指针被传到fun()函数中,对字符串进行操作
    return 0;
}

void fun (char *a)                                    //因为传入的是char *类型,故在调用函数部分必须要用char *类型接住
{
    int flage;                                        //fun()函数内部定义了一个变量
    for (int start_flage = 0, end_flage = strlen(a)-1; start_flage < end_flage; start_flage++,end_flage--)
            if(a[start_flage] == a[end_flage])        //for循环语句中也可以定义变量
                flage = 1;
            else
                break;
    if(flage == 1)                                    //设置标志位的目的是进行何种输出
        printf("this is    Huiwei\n");
    else
        printf("this is not Huiwei\n");
}
/*
    整型数组的获取方式有很多种:
    1、定义的同时可以赋初值
    2、如果定义的时候不赋初值,则只能用for 循环语句单个依次输入
    字符数组的获取方式
    1、采用gets()函数获取
    2、scanf("%s",a)   注:数组名a相当于指针,也相当于变量取地址
    指针类似索引标记,与下标变量不同的是,下标变量只是作为标记,而指针不仅作为标记,并且对其操作会改变对应空间的内容
    在VC++6.0中显示的结果:
    ————————————————————————
    please input a string :level
    this is Huiwei

    please input a string :WERTYH
    this is not Huiwei
    ————————————————————————
*/
 

    

十二、字符串t放在字符串s的尾部  

/*
    2017年3月13日10:48:44
    功能:不用strcat函数将字符串t放在字符串s的尾部
*/
#include "stdio.h"
int main (void)
{
    char s[100];
    char t[100];
    printf("please input a  string:    ");
    gets(s);
    printf("please input a  string:    ");
    gets(t);
    char *ps = s;
    char *pt = t;
    while (*ps)                               //将指针移动到需要插入的地方
    {
        ps++;
    }
    while (*pt)
    {
        *ps = *pt;
        ps++;
        pt++;
    }
    *ps = '\0';                                //此时字符串s的容量扩充了,原本的结尾标识符被覆盖,故需要重新加上结尾标识符形成新的字符串
    
    puts(s);

    return 0;
}
/*
    总结:
    在VC++6.0中显示的结果:
    ————————————————————————
    please input a  string:    abcdefghijk
    please input a  string:    ASDFGTYU
    abcdefghijkASDFGTYU
    ————————————————————————
*/
 

  

十三、在指定的位置截取指定长度的字符串

 方法一:

/*
    2017年3月14日07:26:43
    功能:在指定的位置截取指定长度的字符串
*/
#include"stdio.h"
void substr(char *, int, int);                                        //在函数声明部分只需要注明数据类型
int main ()
{
    char s[100];
    int startloc, length;

    printf ("please input a string: ");
    gets(s);
    printf ("please input the startloc and length of subtring: ");
    scanf("%d %d",&startloc,&length);

    substr(s, startloc, length);                                     //此时调用函数不需要注明数据类型,只需要传入变量即可

    return 0;
}

void substr(char *s, int startloc, int length)
{
    char *ps = s;                                                    //定义一个指针变量,目的为了找到指定位置上元素
    char *pps = s;                                                   //此时定义的指针变量,目的是找到数组的首地址

    for (int i = 0; i < startloc; i++)
        ps++;
        ps--;                                                        //循环结束后,指针还向后移动了一位,故为了找到指定位置,需要向前移动一位
        while (length)                                               //因为只需要截取指定长度,故当长度为零时,跳出循环
        {
            *pps = *ps;
            pps++;
            ps++;
            length--;
        }
        *pps = '\0';                                                 //但是如果想要形成一个字符串,此时必须加入结尾标识符'\0'
        puts(s);
}
/*
    总结:
    在VC++6.0中显示的结果:
    ——————————————————————————————
    please input a string: 123456789
    please input the startloc and length of subtring: 3 6
    345678
    ——————————————————————————————
*/
 

 方法二:

#include "stdio.h"
void substr(char *char, int startloc, int len);
int main ()
{
    char str[100];
    printf ("please input a string : ");
    gets(str);
    printf("please input location and the length of substring : ");
    int startloc, length;
    scanf("%d %d",&startloc, &length);
    substr(str, startloc, length);
}

void substr(char *str, int startloc, int len)
{
    int n = 0;
    char new_str[100];
    for (int i = startloc; len > 0; i++)
    {
        new_str[n++] = str[i];
        len -=1;
    }
    
    new_str[n] = '\0';
    puts(new_str);
}
 

  

十四、在指定的地方以及想要逆置元素个数

/*
    2017年3月13日08:57:37
    功能:在指定的地方以及想要逆置元素个数
*/
#include"stdio.h"
int *revese(int *, int, int);                  //指向函数的指针,在调用函数中数据改变,将会传给主函数
int main()
{
    int a[100] = { 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 };
    int position;
    int amount;
    printf("请输入开始逆置位置和个数用空格隔开:");                
    scanf("%d %d", &position, &amount);                        
    int *b = reverse(a, position, amount);                                            
    for (int x = 0; x < 10; x++)                //遍历输出字符数组中所有的元素
    {
        printf("%d  ",b[x]);
    }
}
int *reverse(int *a, int position, int amount)
{
    int i = position - 1;
    int j = amount+i -1;
    int t;
    int x = amount/2;
    for (; x > 0; i++, j--)
    {

        t = a[j];
        a[j] = a[i];
        a[i] = t;
        x--;

    }
    return a;

}
/*
    总结
    在VC++6.0中显示的结果:
    ————————————————————————
    请输入开始逆置位置和个数用空格隔开:4 6
    2  4  6  18  16  14  12  10  8  20  
    ————————————————————————
*/
 

  

十五、将第m个字符后面的字符串全部存放在另外一个数组中

/*
    2017年3月12日08:56:37
    功能:将第m个字符后面的字符串全部存放在另外一个数组中(指针实现)
*/
#include "stdio.h"
int main (void)
{
    int m;
    int i = 0;
    char a[100];
    char b[100];
    printf("please input a string :\n");        //注意字符串的输入要在输入数字之前才能用gets();如果两者之间的顺序改变了,就会影响结果的输入。
    gets(a);
    printf ("please input a number :\n");        //如果采用scanf("%s")输入字符串是,末尾指针需要添加结尾标识符'\0'
    scanf ("%d",&m);                        
    char *pb = b;
    char *pa = a;
    while (*pa)
    {
        i++;
        if(i == m)
            break;
        pa++;
    }
    while (*pa)
    {
        *pb = *pa;
            pa++;
            pb++;
    }
    *pb = '\0';

    puts(b);

    return 0;
}
/*
    总结:
    在VC++6.0中显示的结果:
    ——————————————————
    please input a string :
    asdfgh
    please input a number :
    4
    fgh
    ——————————————————
*/
 

  

十六、将第m个字符后面的字符串全部存放在另外一个数组中

/*
    2017年3月12日08:56:37
    功能:将第m个字符后面的字符串全部存放在另外一个数组中(数组实现)
*/
#include"stdio.h"
#include"string.h"
int main()
{
    int n;
    char str[100];
    char str1[100];
    int j = 0;
    gets(str);
    scanf("%d", &n);
    for (int i = n - 1; i < strlen(str); i++)
    {
        str1[j++] = str[i];
    }
    str1[j] = '\0';                                //因为是字符串存入到新数组空间中,末尾没有结尾标识'\0',故需要加入一个'\0'
    puts(str1);
}

/*
    总结:
    在VC++6.0中显示的结果:
    ——————————————————
    please input a string :
    asdfgh
    please input a number :
    4
    fgh
    ——————————————————
*/
 

  

十七、字符是否与字符串中的某个字符相同,相同不做任何操作,不同则插在字符串末尾

/*
    2017年3月8日23:23:43
    功能:字符是否与字符串中的某个字符相同,相同不做任何操作,不同则插在字符串末尾
*/
#include"stdio.h"
int main()
{
    char str[100];
    char ch;
    printf("please input a string:");
    gets(str);
    printf("please input a char:");
    scanf("%c",&ch);
    char *pstr = str;
    while(*pstr)
    {
        if(*pstr != ch)
        {
            pstr++;
            continue;
        }
        else 
            break;
    }
    if(*pstr)
        ;
    else
    {
        pstr--;
        *pstr = ch;
    }      
    puts(str);
}
/*
    总结:
    在VC++6.0中显示的结果:
    ——————————————————————————————————
    please input a string:ffghjyhkkkluio
    please input a char:a
    ffghjyhkkkluia
    ——————————————————————————————————
*/
 

 

十八、把字符串的内容逆置

/*
    2017年3月5日14:48:11
    功能:把字符串的内容逆置
*/

#include"string.h"
#include "stdio.h"
#define N 81
void fun(char *s)
{
    int i, n = strlen(s)-1;                        //strlen()是求字符串全部元素个数包括最后的'\0'(字符串结尾的字符),而strlen()-1表达式所求得是数组最大下标值
    char t;
    for (i = 0; i < n; i++, n--)                //strlen()是求字符串的长度,注意区分它不是求数组的长度,即char型数组,不适用int型数组。
    {
        t = s[i];
        s[i] = s[n];
        s[n] = t;
    }
} 

int main ()
{
    char a[N];
    printf ("请输入一个字符串:\n");
    gets (a);                                    //当一次输入一个字符串时用gets()函数
    fun(a);
    printf ("修改后的字符串是: \n");
    puts (a);                                    //当一次输出一个字符串时用puts()函数 

    return 0;
}
/*
    总结:
    1、getchar ()函数的用处是输入单个字符
    2、scanf ()函数的用处是输入整数型,浮点数型的数据
    3、在VC++6.0中显示的结果:
    ————————————————————————
    请输入一个字符串:
    asdfghj
    修改后的字符串是:
    jhgfdsa

    ————————————————————————
*/

 

十九、区分字符串中的字符类型

/*
	2017年6月30日14:03:13
	功能:输入一个字符串,将里面的数字、字母、其他符号筛选出来!
*/
# include <stdio.h>
# include <string.h>

void main(void)
{
 char *p,str[20], sz[20], zm[20], qt[20];
 int i = 0, n, a, b, c;
 a = b = c = 0;
 printf("Input string:");
 gets(str);
 p = str;
 n = strlen(str);											//求取字符串的长度,注意:strlen函数是不包括‘\0’的长度的,sizeof计算的结果才包括'\0'的长度
 while(i<n)
 {
  if (*(p+i)>='0'&&*(p+i)<='9')
   sz[a++] = *(p+i);
  else if(*(p+i)>='a'&&*(p+i)<='z'||*(p+i)>='A'&&*(p+i)<='Z')
   zm[b++] = *(p+i);
  else
   qt[c++] = *(p+i);
	i++;
 }
 sz[a] = '\0'; zm[b] = '\0'; qt[c] = '\0';					//添加串结束标志;
 printf("请输出只含有数字的字符串:");
 puts(sz);													//输出数字字符;
 printf("请输出只含有字母的字符串:");
 puts(zm);													//输出字母字符;
 printf("请输出只含有其他字符的字符串:");
 puts(qt);													//输出其他字符
}
/*
在VC++6.0中显示的结果:
--------------------------
Input string:abc52#*69JKL13
请输出只含有数字的字符串:526913
请输出只含有字母的字符串:bcJKL
请输出只含有其他字符的字符串:#*
--------------------------
*/

  

  

 

posted @ 2017-07-01 15:05  樱桃挚爱丸子  阅读(268)  评论(0编辑  收藏  举报