0

Writing a program that asks for user input of 3 strings. The program then takes each string and counts the number of digits, lower case letters, and punctuation. Program compiles but after the user enters the first string, there is a segmentation fault and the program crashes. When I run the program this is the result:

Enter the first string :1 Enter the second string :Enter the second string :2 Segmentation fault

Not sure exactly what is happened but any help would be awesome! Thanks.

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

int countDigit(char *s);
int countLower(char *s);
int countPunct(char *s);

int main (void)
{
        int digit, lower, punct;
        char *first;
        char *second;
        char *third;
        char *c;

        printf("Enter the first string\n:");
        scanf("%99s", first);
        printf("Enter the second string\n:");
        scanf("%99s", second);
        printf("Enter the second string\n:");
        scanf("%99s", third);

        digit=countDigit(first);
        lower=countLower(first);
        punct=countPunct(first);

        printf("There are %d digits, %d lower case letters and %d punctuation chars in a\n", digit, lower, punct);

        digit=countDigit(second);
        lower=countLower(second);
        punct=countPunct(second);

        printf("There are %d digits, %d lower case letters and %d punctuation chars in b\n", digit, lower, punct);

        digit=countDigit(third);
        lower=countLower(third);
        punct=countPunct(third);

        printf("There are %d digits, %d lower case letters and %d punctuation chars in c\n", digit, lower, punct);

}

int countDigit(char *s)
{
        int count = 0;
        char temp;
        while(*s !=0)
        {
                temp = *s;
                if (isdigit(temp))
                {
                        count++;
                }
                s++;
        }
        return count;
}

int countLower(char *s)
{
        int count = 0;
        char temp;
        while (*s !=0)
        {
                temp = *s;
                if (islower(temp))
                {
                        count++;
                }
                s++;
        }
        return count;
}

int countPunct(char *s)
{
        int count = 0;
        char temp;
        while (*s !=0)
        {
                temp = *s;
                if (ispunct(temp))
                {
                        count++;
                }
                s++;
        }
        return count;
}
Sara Tine
  • 39
  • 7
  • Just a suggestion for future questions. You should try to keep your questions [short but self contained](http://www.sscce.org/). Since you get a **Segmentation Fault** after the input you can be sure that the Problem is in the beginning and everything else could be left away. Also your question got no c++ elements in it, so that tag shouldn't be used or you might get c++ answers which you can't use if you're in fact using c. – AliciaBytes Mar 21 '14 at 02:47
  • Looks a lot like [this question from yesterday](http://stackoverflow.com/questions/22527152/strcmp-giving-segmentation-fault/22527224?noredirect=1#comment34279804_22527224) – codah Mar 21 '14 at 03:18

2 Answers2

3
char *first;
char *second;
char *third;
char *c;

printf("Enter the first string\n:");
scanf("%99s", first);
printf("Enter the second string\n:");
scanf("%99s", second);
printf("Enter the second string\n:");
scanf("%99s", third);

You never allocated memory for the strings. All your pointers point to some random memory addresses, so writing to them is undefined behavior.

AliciaBytes
  • 7,300
  • 6
  • 36
  • 47
3

You need to allocate memory for your strings. What you have there are only pointers, you need to do the following:

char *first  = new char[100];
char *second = new char[100];
char *third  = new char[100];

That way, you have allocated memory from the heap where you can store the input from users. Make sure you free the memory when you're done with it:

delete [] first;
delete [] second;
delete [] third;

Note that new and delete are from C++. You'd need to include the appropriate headers, or use the C equivalents malloc and free.

You can also use memory from the stack, which is easier to deal (you don't have to free it) with but also more scare, like this:

char first[100];
char second[100];
char third[100];

Basically, when you try to access a memory address without first allocating memory, you get a segmentation fault. It's just a safety mechanism in order to prevent (more) bugs. When you need memory, make sure you either use memory from the stack, or that you allocate it with C malloc or C++ new.

And, just as a suggestion, if you're using C++, it wouldn't hurt to take a look at std::string.

Community
  • 1
  • 1
ArthurChamz
  • 2,039
  • 14
  • 25
  • 2
    Or, simply stick to a simpler, faster and safer stack allocation by doing char first[100], second[100], third[100]. – DigitalEye Mar 21 '14 at 02:46