删除字符串中的指定字符
2014-03-02 17:15
477 查看
已知输入字符串,要求输入指定要删除已知字符串中的具体字符。
比如说:已知字符串为thankyou,指定输入4,则会删除原字符串中的k,得到thanyou
我在程序中添加了一项if判断,限定了输入元素n的范围,n必须>=0且<=元素的总个数。
如果输入的n<0||n>count,会返回-1,结束程序。运行如下:
或者:
比如说:已知字符串为thankyou,指定输入4,则会删除原字符串中的k,得到thanyou
#include <stdio.h> #include <conio.h> #include <stdlib.h> #define N 20 void func(char a[],char b[],int n) { int i,k = 0; for (i = 0; a[i] != '\0'; i++) { if (i != n) { b[k++] = a[i]; } } b[k] = '\0'; } int main() { system("CLS"); int n; char str1 ,str2 ; printf("Please enter a string :\n"); gets(str1); int i = 0,count = 0; while(str1[i] != '\0') { i++; count++; } printf("Enter the position of the string deleted :\n"); scanf("%d",&n); if((n < 0)||(n >= count)) { printf("n must larger or equal to 0 and less than the size of input number \n"); getch(); return -1; } func(str1,str2,n); printf("The new string is : %s",str2); getch(); return 0; }结果如下:
我在程序中添加了一项if判断,限定了输入元素n的范围,n必须>=0且<=元素的总个数。
如果输入的n<0||n>count,会返回-1,结束程序。运行如下:
或者:
相关文章推荐
- 在一个字符串中删除指定的字符 & 删除字符串中所有出现在另一个字符数组中的字符
- 在一个给定的字符串中删除指定的字符
- python 删除字符串中指定字符&去掉空格
- java删除字符串中的指定字符
- C#中,删除字符串中的指定字符的简便方法
- 字符串操作之删除指定字符
- 程序员面试指南(第六章) 数组和字符串(C#版) 2.从字符串中删除指定的字符
- c++在字符中删除指定字符串
- 删除字符串中指定的字符,如果字符前后有指定的标点符号也一并删除
- 删除字符串中指定位置的字符
- 在制定字符串内删除指定字符。
- Exe7 删除字符串中指定的字符 (还有有问题——指针的问题)
- 删除字符串中的指定字符
- 从字符串指定位置删除指定长度字符
- 字符串的一些基本操作,复制,长度,子串查找,单词反转,比较,字符串反转,删除指定字符。
- 编程实现删除字符串中所有指定的字符
- 在字符串中删除指定字符
- 字符串处理的两个问题:删除指定字符,删除相邻相同的字符
- 删除字符串中指定字符
- 删除字符串中指定字符