分割字符串
2012-06-02 11:13
113 查看
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream.h>
#include<conio.h>
int parsestring(char *p,char i,char *a,char *b)
{
bool flag = true;
int index = 0;
while(*p != '\0')
{
if(flag)
{
while(*p != i)
{
index++;
*a++ = *p++;
}
flag = false;
}
else
{
*b++ = *p++;
}
}
*a = '\0';
*b = '\0';
return index;
}
int main()
{
char c1;
char s2[50] = {'\0'};
char s3[50] = {'\0'};
char str1[50] = {'\0'};
int index = 0;
#if 0
printf("*******请输入一字符串:*********\n");
cin>>str1;
printf("*******请输入需要分割的字符:**********\n");
cin>>c1;
#else
printf("*******请输入一字符串:*********\n");
scanf("%s",str1);
printf("*******请输入需要分割的字符:**********\n");
//scanf("%c",&c1);
c1 = getch();
#endif
index = parsestring(str1,c1,s2,s3);
printf("前一段字符串是%s, 分割符索引号是%d,后一段字符串是%s.\n",s2,index,s3);
return 0;
}
#include<stdlib.h>
#include<string.h>
#include<iostream.h>
#include<conio.h>
int parsestring(char *p,char i,char *a,char *b)
{
bool flag = true;
int index = 0;
while(*p != '\0')
{
if(flag)
{
while(*p != i)
{
index++;
*a++ = *p++;
}
flag = false;
}
else
{
*b++ = *p++;
}
}
*a = '\0';
*b = '\0';
return index;
}
int main()
{
char c1;
char s2[50] = {'\0'};
char s3[50] = {'\0'};
char str1[50] = {'\0'};
int index = 0;
#if 0
printf("*******请输入一字符串:*********\n");
cin>>str1;
printf("*******请输入需要分割的字符:**********\n");
cin>>c1;
#else
printf("*******请输入一字符串:*********\n");
scanf("%s",str1);
printf("*******请输入需要分割的字符:**********\n");
//scanf("%c",&c1);
c1 = getch();
#endif
index = parsestring(str1,c1,s2,s3);
printf("前一段字符串是%s, 分割符索引号是%d,后一段字符串是%s.\n",s2,index,s3);
return 0;
}
相关文章推荐
- 一个简单程序引起的反思----算字符串最后一个单词的长度,单词以空格分割
- SQLserver字符串分割函数
- 字符串分割自定义函数(SQL)
- bash 分割字符串
- java String.split 分割字符串的方法
- 如何从一段字符串中根据分个字符分割出数据
- java截取字符串,截串,substring和split,分割字母和数字,正则缝隙
- 分割字符串,转换成列表数据
- C语言字符串分割——strtok
- java中split()方法分割字符串后数组长度问题
- JS使用split分割字符串并应用正则匹配
- Python 字符串操作(string替换、删除、截取、复制、连接、比较、查找、包含、大小写转换、分割等)
- split的坑-字符串分割
- split()分割字符串
- 字符串分割--java中String.split()用法
- c#中分割字符串的几种方法
- Split()函数,不传入字符,传入字符串分割字符串
- 利用MySQL存储过程分割字符串
- 一个分割字符串的函数
- 去除Split分割后的空字符串