1

I'm trying to run an isalpha check on an entered string but the issue is, that isalpha works only for individual characters apparently. If I run it like this on a string, I get a segmentation fault.

There might be a more elegant solution, but I can not find a way to connect the string with the char array which is the only missing piece

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

int i;

int main (void)    
{
    
    string text = get_string("Text: \n");

    int lenght = strlen(text);

    if(isalpha(text))
    {
        printf("Well done");
    }
    else
    {
        printf("You suck");
    }

So I tried to transform the string into each individual char array. dispate the fact that there might be a more elegant solution, I can not find a way to connect the string with the char array which is the only missing piece

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

int i;

int main (void)    
{
    
    string text = get_string("Text: \n");
    int lenght = strlen(text);
    char letter[lenght];
    

    for(i = 0; i < lenght; i++)
    {
        
        printf("Letter %i is %c\n", i, letter[i]);

    }

}

Any piece of advice how I can run the isalpha check on my string before I continue to the actual function?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Joesns
  • 45
  • 5
  • It is unclear what you are trying to do. Do you need to check that the whole string consists from letters? – Vlad from Moscow Dec 31 '20 at 11:55
  • exactly. I am trying to run a check on wether the string is alphabetical or not, using the isalpha command from the ctype library – Joesns Dec 31 '20 at 11:56
  • 1
    Confusing cs50.h strikes again. `string` IS already a char*. It's just an alias. – Ivan C Dec 31 '20 at 11:57
  • I thought so too, but if I run the isalpha command on a string, I get a segmentation fault error. – Joesns Dec 31 '20 at 12:01
  • @Joesns you should get a compilation error if you attempt `isalpha(text)`. If not then please adjust your compiler settings as using the wrong settings is causing you to waste time by running invalid code – M.M Jan 13 '21 at 21:03

2 Answers2

1

Just write a function that will perform such a check.

It can look the following way as it is shown in the demonstrative program below.

#include <stdio.h>
#include <ctype.h>

int is_all_alpha( const char *s )
{
    while ( *s && isalpha( ( unsigned char )*s ) ) ++s;
    
    return *s == '\0';
}

int main(void) 
{
    char *s1 = "Hello";
    char *s2 = "2021";
    
    printf( "\"%s\" is %sa valid word\n", s1, is_all_alpha( s1 ) ? "" : "not " );
    printf( "\"%s\" is %sa valid word\n", s2, is_all_alpha( s2 ) ? "" : "not " );

    return 0;
}

The program output is

"Hello" is a valid word
"2021" is not a valid word

Or using the definition of the name string the program can look like

#include <stdio.h>
#include <ctype.h>
#include <cs50.h>

int is_all_alpha( string s )
{
    while ( *s && isalpha( ( unsigned char )*s ) ) ++s;
    
    return *s == '\0';
}

int main(void) 
{
    string s1 = "Hello";
    string s2 = "2021";
    
    printf( "\"%s\" is %sa valid word\n", s1, is_all_alpha( s1 ) ? "" : "not " );
    printf( "\"%s\" is %sa valid word\n", s2, is_all_alpha( s2 ) ? "" : "not " );

    return 0;
}

Though it is much better to declare the function parameter as having the type const char * instead of string because within the function is_all_alpha the pointed string is not changed. And the type const string is not the same as the type const char *. The type const string is an alias for the type char * const that is it means that the passed pointer itself is constant not the string pointed to by the pointer.

Instead of the conditional operator used in the calls of printf you can use if-else statements. For example

if ( is_all_alpha( text ) )
{
    // all symbols of text are letters
    // do something
}
else
{
    // text contains a non-alpha symbol
    // do something else
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Thanks for the solution Vlad. It is probably a little too advanced yet, but this is something I will definitely look more into in the next couple of days. – Joesns Dec 31 '20 at 12:19
0

In CS50's header, they typedef string to char*. So, your string is already a char array, and there's no need to convert it. You could use a simple strlen construction:

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

int main (void) {
    string text = get_string("Text: \n");
    int len = strlen(text);
    
    bool allAlpha = true;
    for(int i = 0; i < len; i++) {
        if (!isAlpha(text[i])) {
            allAlpha = false;
            break;
        }
    }
    if (allAlpha) {
        printf("Everything's alphabetical.\n");
    } else {
        printf("There's a non-alphabetical character.");
    }
}

Although, this has to loop through the string twice, since strlen loops through the whole array. One thing you can do is to advance the pointer, and keep going until you find the null byte at the end:

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

int main (void) {
    string text = get_string("Text: \n");
    
    bool allAlpha = true;
    for(char* ptr = text; *ptr != '\0'; ptr++) {
        if (!isAlpha(*ptr)) {
            allAlpha = false;
            break;
        }
    }
    if (allAlpha) {
        printf("Everything's alphabetical.\n");
    } else {
        printf("There's a non-alphabetical character.");
    }
}

The != '\0' is frequently omitted, since \0 is considered as false (due to being zero), and everything else is considered as true.

Aplet123
  • 33,825
  • 1
  • 29
  • 55
  • @Joesns I'm glad I could help you! If you're satisfied, please [mark the answer as accepted](https://doc.rust-lang.org/reference/items/use-declarations.html#use-visibility) so people know that the question's been answered. – Aplet123 Dec 31 '20 at 12:21
  • `isAlpha` is not defined – M.M Jan 13 '21 at 21:04