[英]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).例如,如果我输入abc123
作为我的字符串,程序应该告诉我有 3 个字母并且数字的总和是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.但是,如果我输入一个字母数字字符串,例如"abc123"
则会出现分段错误。 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.我认为这个问题与我的循环逻辑有关,但我不是 100% 确定。
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
.每个都将currentIndex
传递给另一个,但每个都忽略它从另一个获取的currentIndex
并从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.因此,如果您给它一个字符串,该字符串具有从字母到数字的转换(例如“g3”),则这两个函数会相互调用,直到它们使调用堆栈过载为止。
One solution: have the functions use currentIndex
.一种解决方案:让函数使用currentIndex
。
Another solution: handle letters and numerals in one function.另一种解决方案:在一个函数中处理字母和数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.