电话面试题
C#
给你一个string字符串, abcd as,ccd.dsa
反转这个字符串变为: dsa.ccd,as abcd
要求:不能用【任何】【系统函数】。
[解决办法]
加载到数组,从下标向上标读取.
[解决办法]
从后往前遍历,组合呗
或者,将每个字符压栈,然后遍历栈,所以依次取出就翻转,比如
string ss = "abcdefg"; Stack<char> stack = new Stack<char>(); foreach (char c in ss) { stack.Push(c); } ss = ""; foreach (char c in stack) { ss += c.ToString(); } MessageBox.Show(ss):或者 string ss = "abcdefg"; string des = ""; for(int i=ss.Length-1;i>=0;i--) { des += ss[i].ToString(); } MessageBox.Show(des);
[解决办法]
入栈出栈即可
[解决办法]
string oldStr = "abcd as,ccd.dsa"; string cutStr = " ,."; string tempStr = ""; string re = ""; for (int i = oldStr .Length - 1; i >= 0 ;i-- ) { char c = oldStr [i]; bool iscut = false; foreach (char cut in cutStr) { if(cut == c){iscut = true;continue;} } if(iscut){re = tempStr + c + re;tempStr = "";} else{tempStr = c + tempStr;} } return tempStr + re;
[解决办法]
电话面试,别人主要看你的反应能力和解决问题的思路,面试者自己都不一定能写出来都是网上找的鬼题目。
[解决办法]
希望这个可以帮到你:
private void Form1_Load(object sender, EventArgs e) { string[] aa = { "wo","ai","kan","mao","pian"}; string temp = ""; for (int i = 0; i < aa.Length / 2;i++) { temp = aa[aa.Length - i - 1]; aa[aa.Length - i - 1] = aa[i]; aa[i] = temp; } for (int j = 0; j < aa.Length; j++) { MessageBox.Show(aa[j]); }//依次弹出:pian,mao,kan,ai,wo }
[解决办法]
class stack:IStack
{
stack s;
stack next;
private string data;
public string Data
{
get { return data; }
set { data = value; }
}
public static int len = 0;
public void push(string data)
{
next = new stack();
next.data = data;
len++;
if (len > 0)
next.next = s;
else
{
s = new stack();
s.data = data;
}
s = next;
}
public string pop()
{
string str = s.data;
if(s.next!=null)
s = s.next;
return str;
}
}
public static string rever(string str)
{
stack s = new stack();
string temp="";
for (int i = 0; i < str.Length; i++)
{
if (str[i] != ',' && str[i] != ' ' && str[i] != '.'&&i<str.Length-1)
temp += str[i];
else
{
if (str.Length - 1 == i)
{
temp += str[i];
s.push(temp);
}
else
{
s.push(temp);
s.push(str[i].ToString());
}
temp = "";
}
}
string strprint = "";
for (int i = 0; i < stack.len; i++)
strprint += s.pop();
return strprint;
}
static void Main(string[] args)
{
Console.WriteLine(rever("abcd as,ccd.dsa"));
}
[解决办法]
可不可以用栈啊,先存进去,在都出来
[解决办法]
string str = "abcd as,ccd.dsa";
string[] result = new string[4];
int i = 0;
foreach (char ch in str)
{
if (ch != ' ' && i<4)
{
result[0] += ch;
}
if (ch != ',' && i > 4 && i<7)
{
result[1] += ch;
}
if (ch != '.' && i > 7 && i < 11)
{
result[2] += ch;
}
if (i > 11)
{
result[3] += ch;
}
i++;
}
result[3] += '.';
result[2] += ',';
result[1] += ' ';
int j = 0;
for (j = result.Length - 1; j >= 0; j--)
Console.Write(result[j]);
Console.Read();
[解决办法]
string info = "abcd as,ccd.dsa";
string []temp = new string[7];
int mark=0;
foreach(char i in info)
{
if (i!= ' ' && i != ',' && i != '.')
{
temp[6 - mark] += i;
}
else
{
temp[6 - (++mark)] += i;
mark++;
}
}
foreach (string i in temp)
{
Console.Write(i);
}
[解决办法]
static void Main(string[] args) { string str = "abcd as,ccd.dsa"; Console.WriteLine(CustomReverse(str)); Console.Read(); } static string CustomReverse(string str) { string result = "", temp = ""; for (int i = 0; i < str.Length; i++) { //if (Char.IsLetter(str[i])) if (str[i] != ' ' && str[i] != ',' && str[i] != '.') { temp += str[i]; } else { result = str[i] + temp + result; temp = ""; } } result = temp + result; return result; }
[解决办法]
字母不反转,非字母反转
public static void main(String[] args) { System.out.println(reverse("asd,fsdf.gfg gdg".toCharArray())); System.out.println(reverse("9ujf093u4fj.,,mskldjf".toCharArray())); System.out.println(reverse("j.,/m".toCharArray())); } public static char[] reverse(char[] source) { int len = source.length, t = 0; char[] target = new char[len]; for (int i = 0; i < len; i++) { if (!isWord(source[i])) { copy(source, target, t, i); t = i + 1; target[len - i - 1] = source[i]; } } copy(source, target, t, len); return target; } public static boolean isWord(char c) { if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) return true; else return false; } public static void copy(char[] source, char[] target, int start, int end) { if (start >= end) { return; } int len = source.length - start - end; for (int i = start; i < end; i++) { target[i + len] = source[i]; } }
[解决办法]
这样的题目都能把你们给纠结的?有没搞错!
说这种题目没意义的只说明你没做过真正的软件!只是在增删改
[解决办法]
今天儿童节。
static void Main(string[] args) { string str = "Hello, you are the $#@ god*&^of^ the&world!"; Regex reg = new Regex("^[A-Za-z]$"); List<string> result = new List<string>(); string word = string.Empty; foreach (var c in str.ToArray()) { if (reg.IsMatch(c.ToString())) { word += c; } else { result.Add(word); result.Add(c.ToString()); word = string.Empty; } } result.Add(word); for (int i = result.Count - 1; i >= 0; i--) { Console.Write(result[i]); } Console.ReadKey(); }
[解决办法]
string str = "abcd as,ccd.dsa"; //output "dsa.ccd,as abcd" Regex regexNotLetter = new Regex(@"[^a-z]"); Regex regexLetter = new Regex(@"[a-z]+"); Stack stack = new Stack(); Console.WriteLine("start :" + str); var letterMatches = regexLetter.Matches(str); var notLetterMatches = regexNotLetter.Matches(str); for (int i = 0; i < letterMatches.Count; i++) { stack.Push(letterMatches[i]); if (i < notLetterMatches.Count) { stack.Push(notLetterMatches[i]); } } Console.Write("result : "); while (stack.Count > 0) Console.Write(stack.Pop()); Console.Read();
[解决办法]
How about this?
string ss = "ab,cd.ee ef";Regex regex = new Regex("([a-zA-Z]+)([,. ]|$)");Match match = regex.Match(ss);StringBuilder result = new StringBuilder();while (match.Success){ result.Insert(0, match.Result("$1")); result.Insert(0, match.Result("$2")); match = match.NextMatch();}Console.WriteLine(result.ToString());
[解决办法]
C++源代码奉上
#include <iostream.h>#include <string.h>void main(){char s[90]="zhycheng hello world!";int v=strlen(s);int half=v/2;for(int i=0;i<half;i++){ char temp=*(s+i); *(s+i)=*(s+v-i-1); *(s+v-i-1)=temp; }