In: Computer Science
Please, write code in c++. Using iostream and cstring library
Use pointers!
You given a text.Your task is to write a function that will find
the longest sequence of digits inside.
Note that the output have to be presened just like in sample.
Note. The program have to use pointer.
Input:
First line contains one line that is not longer than 1000.
Output:
The longest sequence of numbers.All numbers are positive and
integers.
Samples:
№ | INPUT | OUTPUT |
1 | This is the longest - 10001 number | 10 001 |
2 | 101 fdvnjfkv njfkvn fjkvn jffdvfdvfd2010 | 2 010 |
2 | 1a11 | 11 |
WHOLE PROGRAM IN C++
#include <iostream>
#include<cstring>
using namespace std;
void function(char str[])
{
char ans[1000];
int k=0;
int max=0;
for(int i=0;str[i]!='\0';i++)
{
if(*(str+i)>='0'&& *(str+i)<='9')
{
char t[500];
int s=0;
int c=0;
for(int j=i;*(str+j)!='\0';j++)
{
if(*(str+j)>='0'&& *(str+j)<='9'){
t[s++]=*(str+j);
c++;
}
else
break;
}
if(max<c)
{
k=0;
for(int i=0;i<s;i++)
ans[k++]=*(t+i);
max=c;
}
}
}
char temp[k];
strcpy(temp,ans);
int index=0;
int count=0;
for(int i=k-1;i>=0;i--)
{
if(count==3)
{
temp[index++]=' ';
temp[index++]=*(ans+i);
count=0;
}
else
temp[index++]=*(ans+i);
count++;
}
//printing the results using pointers
for(int i=index-1;i>=0;i--)
cout<<*(temp+i);
}
int main() {
char str[1000];
cin.getline(str, 1000);
function(str);
return 0;
}
EXAMPLE 1 :
EXAMPLE 2 :
EXAMPLE 3 :