Нужно записать индекс числа из текста в новый массив. Само число получается записать, а его индекс нет.
В чем проблема?
#include<iostream>
#include<iomanip>
#include<cstring>
using namespace std;
char* zagl(char*str1)
{
int i, index, k = 0;
char* a = new char[2];
for (i = 0; str1[i] !='\0'; i++)
{
if (str1[i] >= '0' && str1[i] <= '9')
{
a[k++] = str1[i];
}
}
return a;
}
int main()
{
setlocale(LC_ALL, "Russian");
char* str1, *a;
str1 = { "Today is Seventy 7 Four April\n" };
cout <<"Строка:\n"<< str1;
cout << " Индекс:\n";
a = zagl(str1);
cout << a;
cout << "\n Сортировка по алфавиту:\n";
sort(a);
cout << a<<endl;
}
#include <algorithm>
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
system("chcp 1251 > nul");
string line{ "Today is the twenty 7 of April" };
const auto pos = find_if(line.begin(), line.end(), isdigit);
if (pos != line.end()) {
auto beg = pos;
cout << "Число: ";
while (isdigit(*beg)) {
cout.put(*beg);
++beg;
}
cout << "\nИндекс: " << distance(line.begin(), pos) << '\n';
}
system("pause > nul");
}