字符串处理问题(c++和c#两种解法)
2011-10-15 22:15
489 查看
![](https://oscdn.geek-share.com/Uploads/Images/Content/202010/29/7435387185f784643858a9873420064c.gif)
c++解法:
#include<iostream> #include<string> #include<algorithm> using namespace std; int main() { string str1,characters,numbers; int count,first,last; char temp; while(cin>>str1&&str1!="**") { count=str1.length(); first=0;last=count; char *p=new char[count]; for(int i=0;i<count;i++) { if(str1[i]>='0'&&str1[i]<='9') { p[first]=str1[i]; first++; } if(str1[i]>='a'&&str1[i]<='z') { p[last]=str1[i]-32; last--; } if(str1[i]>='A'&&str1[i]<='Z') { p[last]=str1[i]; last--; } } if(first==0) cout<<"There are not digits"; else { sort(p,p+first); for(int i=0;i<first;i++) cout<<p[i]; } if(last==count) cout<<"\n"<<"There are not letters"<<"\n"; else { cout<<"\n"; sort(p+last+1,p+count+1);//此处应注意last指针的位置 for(int i=last+1;i<=count;i++) cout<<p[i]; cout<<"\n"; } } return 0; }
c#解法:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ceshi { class Program { public static string s; static bool di = false, le = false; public static char[] digits = new char[80]; public static char[] letters = new char[80]; static void Main(string[] args) { s = Console.ReadLine(); for (int i = 0; i < 80; i++) { digits[i] = '#'; letters[i] = '#'; } if (s != "**") { string s1 = s.ToUpper();//先将字符串中的所有小写字母转换成大写 for (int i = 0; i < s.Length; i++) { if (s1[i] >= '0' && s1[i] <= '9')//字符串带下标相等于char { digits[i] = s1[i]; di = true; } if (s1[i] >= 'A' && s1[i] <= 'Z')//最初模仿c++中的方法将a通过a-32换成A,但在c#中letters[i]=s1[i]-32不可以直接赋值,因此只好在开头通过ToUpper()将小写全部转换为大写,然后直接将大写字母填充到letters中。 { letters[i] = s1[i]; le = true; } } if (!di) Console.Write("There are not digits."); else { Array.Sort(digits);//排序算法,升序(另有方法array.reverse为降序) for (int i = 0; i < 80; i++) { if (digits[i] != '#') Console.Write(digits[i]); } } if (!le) Console.WriteLine("There are not letters."); else { Console.WriteLine(); Array.Sort(letters); for (int i = 0; i < 80; i++) { if (letters[i] != '#') Console.Write(letters[i]); } Console.WriteLine(); } } Console.Read(); } } }
相关文章推荐
- 记录一下C++和C#中的字符串处理与编码格式的问题
- 与字符串有关的一些典型问题的C++解法
- C/C++字符串处理函数,学会了就会简化好多问题
- 一个关于c++字符串处理和delete[]与delete差别的问题
- 汉明距离(Hamming Distance)问题的两种解法,C++实现
- C#调用C++ DLL 传出字符串,C++ DLL传参问题
- C++用纯字符串来处理高精度问题代码
- C++第六周任务一【任务1】下面的程序存在编译错误。有两种方法可以修改,请给出这两种修改方案,在报告中说明你倾向于用哪一种?为什么?处理此类问题的原则是什么?
- C#对于字符串的处理类(剪裁、过滤危险字符、替换sql中有问题符号等)
- c/c++ 处理字符串时要注意的问题
- C# 调用 C++ dll 函数 时传递字符串 需要注意的问题
- C#字符串数组排序 C#排序算法大全 C#字符串比较方法 一个.NET通用JSON解析/构建类的实现(c#) C#处理Json文件 asp.net使用Jquery+iframe传值问题
- 同一问题的两种不同解法 : MFC8.0 与 C++ 标准库
- c#调用C++时处理tchar *转字符串
- C#调用C++Dll字符串返回为空的问题
- 工作中遇到的关于C#字符串处理小问题。《记录经验》
- 蓝桥杯 问题 1094: 字符串的输入输出处理(c++实现)
- C#调运C++的DLL返回中文字符串乱码问题
- c/c++中动态内存分配处理字符串的细节问题
- C#对于字符串的处理类(剪裁、过滤危险字符、替换sql中有问题符号等)