In: Computer Science
Please, write code in c++. Using iostream and cstring library.
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.
example:
input: 101 fdvnjfkv njfkvn fjkvn jffdvfdvfd2010
output: 2010
Code:
temp.cpp
#include<iostream>
#include<cstring>
using namespace std;
int longest_sequence(char* s)
{
int max_digit=0, max_res=0,i=0;
for(i=0;s[i]!='\0';i++)
{
if(s[i]>='0' && s[i]<='9')
max_digit = max_digit*10 + (s[i]-'0');
else
{
max_res = max(max_res,max_digit);
max_digit=0;
}
}
return max(max_digit, max_res);
}
int main()
{
char s[100];
cout<<"Enter the text: ";
cin.getline(s,100);
cout<<"Input :\n"<<s;
cout<<"Output: "<<longest_sequence(s)<<endl;
return 0;
}
Output:
Please
comment in case of doubts or queries.
It would be really helpful if you could upvote
:)