简体   繁体   中英

Add digits and count letters of a string (segmentation fault)

As part of an assignment, I must write a program that gets a string from the user, and prints out the number of letters in that string, as well as the sum of the digits in that string. So for example, if I were to type in abc123 as my string, the program should tell me that there are 3 letters and the sum of the digits is 6 (3+2+1). The program must use corecursive functions (ie the functions call each other). My code so far is as follows:

#include <stdio.h>
#include <string.h>

//Function prototypes
void countAlph(char str[], int, int);
void sumDigits(char str[], int, int);

int main(void)
{
    //Create an array that can hold 25 characters
    char anStr[25];
    int inputSize;

    //Get string from user
    printf("Enter an alphanumeric string:\n");
    scanf("%s", anStr);

    //Get size (# of elements in the array)
    inputSize = strlen(anStr);

    //Function call
    countAlph(anStr, inputSize, 0); //pass the string, size of the string, and 0 (first index position)
}

//function that counts # of letters
void countAlph(char str[], int size, int currentIndex)
{
    //use ASCII codes!

    int alphaCount = 0, i;

    if(currentIndex < size)
    {
        for(i=0; i<size; i++)
        {
            if((str[i] >= 65 && str[i] <= 90) || (str[i] >= 97 && str[i] <= 122)) //if element is a letter
            {
                alphaCount++; //increase # of letters by 1
            }
            else if(str[i] >= 48 && str[i] <= 57) //if element is NOT a letter
            {
                sumDigits(str, size, i); //go to the sumDigits function
            }
        }
    }
    printf("There are %d letters in this string\n", alphaCount);
}

//function that adds all the digits together
void sumDigits(char str[], int size, int currentIndex)
{
    //use ASCII codes!

    int sum = 0, i;

    if(currentIndex < size)
    {
        for(i=0; i<size; i++)
        {
            if(str[i] >= 48 && str[i] <= 57) //if element is a digit
            {
                //I found the line of code below online after searching how to convert from ASCII to int
                str[i] = str[i] - '0'; //this converts the ascii value of the element to the decimal value
                sum = sum + str[i]; //add that digit to the running sum
            }
            else if((str[i] >= 65 && str[i] <= 90) || (str[i] >= 97 && str[i] <= 122)) //if element is NOT a number
            {
                countAlph(str, size, i); //go to the countAlph function
            }
        }
    }
    printf("The sum of the digits of this string is %d\n", sum);
}

My program works correctly if my string contains ONLY numbers or ONLY letters. But if I enter an alphanumeric string such as "abc123" I get a segmentation fault. Any ideas as to what I am doing wrong? I assume the issue is related to my loop logic, but I'm not 100% sure.

Your two functions call each other. Each one passes currentIndex to the other, but each ignores the currentIndex it gets from the other and starts over from i = 0 .

So if you give it a string that has a transitions from letters to numerals (eg "g3") the two functions call each other until they overload the call stack.

One solution: have the functions use currentIndex .

Another solution: handle letters and numerals in one function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM