这是一个控制台应用程序,获取字符下标,为什么都是零?如果indexOf("g")这样的话才会返回
1个回答
展开全部
//newlen 新的数组长度 int* 返回新的数组
char* GetNewArray(char a[],int n,int& newlen)
{
int count = 0;
int i=0;
int j=0;
for (i=0;i<n;i++)
{
int index = 0;
for (j=i+1;j<n;j++)
{
if(a[i] == a[j])
index++;
}
if(index == 0)
count++;
}
char* b = new char[count];
newlen = count;
count = 0;
for (i=0;i<n;i++)
{
int index = 0;
for (j=i+1;j<n;j++)
{
if(a[i] == a[j])
index++;
}
if(index == 0)
{
b[count] = a[i];
count++;
}
}
return b;
}
int main()
{
char a[] = {'a','b','c','d','a','h','a','g','o'};
int len = 0;
char* b = GetNewArray(a,9,len);
for (int i=0;i<len;i++)
{
cout<<b[i]<<endl;
}
getchar();
return 0;
}
char* GetNewArray(char a[],int n,int& newlen)
{
int count = 0;
int i=0;
int j=0;
for (i=0;i<n;i++)
{
int index = 0;
for (j=i+1;j<n;j++)
{
if(a[i] == a[j])
index++;
}
if(index == 0)
count++;
}
char* b = new char[count];
newlen = count;
count = 0;
for (i=0;i<n;i++)
{
int index = 0;
for (j=i+1;j<n;j++)
{
if(a[i] == a[j])
index++;
}
if(index == 0)
{
b[count] = a[i];
count++;
}
}
return b;
}
int main()
{
char a[] = {'a','b','c','d','a','h','a','g','o'};
int len = 0;
char* b = GetNewArray(a,9,len);
for (int i=0;i<len;i++)
{
cout<<b[i]<<endl;
}
getchar();
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询