Ask Question
15 December, 19:17

Write a recursive function called firstOccurence that will return the position of the first occurence of a character within a string B.) Write a recursive function called sumAscii that will return the sum all of the chars within a string. For B the sum of the chars is their ASCII equivalent value. For instance the character 'A' has a value of 65.

+3
Answers (1)
  1. 15 December, 21:11
    0
    C+ + code is explained below

    Explanation:

    a - C+ + code:

    #include

    using namespace std;

    #include

    int firstOccurence (char * s, char c) / /recursive function

    {

    static int i;

    if (! s[i])

    {

    return - 1;

    }

    else

    {

    if (s[i]==c)

    return i;

    i++;

    firstOccurence (s, c);

    } }

    int main ()

    {

    char s[1000], c;

    int n;

    printf ("Enter the string : "); / /tales input

    gets (s);

    printf ("Enter character to be searched: "); / /takes input a character to be searched

    c=getchar ();

    n=firstOccurence (s, c); / /call function

    if (n>-1)

    printf ("character %c is first occurrence at location:%d ", c, n);

    else

    printf ("character is not in the string ");

    return 0;

    }

    b - C+ + Code - This program takes a string and display sum of all characters in a string.

    #include

    using namespace std;

    int sumAscii (string s) / /recursive function

    {

    int sum = 0;

    if (s. length () = = 0)

    {

    exit (0);

    }

    for (unsigned int i = 0; i < s. size (); i++)

    {

    sum + = s[i];

    }

    return sum;

    }

    int main () / /main function

    {

    cout << "This word adds up to " << sumAscii ("CAT") << " in ASCII " << endl;

    return 0;

    }
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “Write a recursive function called firstOccurence that will return the position of the first occurence of a character within a string B.) ...” in 📙 Computers & Technology if there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers