标签:stdio.h char one lease logs asd asc int return
一、字符串的反向输出
/*
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
————————————————————————
*/
七、
/*
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
——————————————————————————————
*/
标签:stdio.h char one lease logs asd asc int return
原文地址:http://www.cnblogs.com/wxt19941024/p/7102417.html