[英]How to convert a string into individual letters char in c
我正在嘗試對輸入的字符串運行 isalpha 檢查,但問題是,isalpha 顯然僅適用於單個字符。 如果我像這樣在字符串上運行它,則會出現分段錯誤。
可能有更優雅的解決方案,但我找不到將字符串與唯一缺失的 char 數組連接起來的方法
#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");
}
所以我嘗試將字符串轉換為每個單獨的 char 數組。 消除可能有更優雅的解決方案的事實,我找不到將字符串與唯一缺失的 char 數組連接起來的方法
#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]);
}
}
在繼續實際的 function 之前,有什么建議可以對我的字符串運行 isalpha 檢查嗎?
只需編寫一個 function 即可執行此類檢查。
它可以如下面的演示程序所示。
#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;
}
程序 output 是
"Hello" is a valid word
"2021" is not a valid word
或者使用名稱string
的定義,程序看起來像
#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;
}
盡管將 function 參數聲明為具有類型const char *
而不是string
要好得多,因為在 function is_all_alpha
中,指向的字符串沒有改變。 並且const string
類型與const char *
類型不同。 const string
類型是char * const
類型的別名,這意味着傳遞的指針本身是常量,而不是指針指向的字符串。
您可以使用 if-else 語句來代替 printf 調用中使用的條件運算符。 例如
if ( is_all_alpha( text ) )
{
// all symbols of text are letters
// do something
}
else
{
// text contains a non-alpha symbol
// do something else
}
在 CS50 的 header 中,他們 typedef string
為char*
。 因此,您的string
已經是一個char
數組,無需對其進行轉換。 您可以使用簡單的strlen
構造:
#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.");
}
}
雖然,這必須循環遍歷字符串兩次,因為strlen
循環遍歷整個數組。 您可以做的一件事是推進指針,並繼續前進,直到在末尾找到 null 字節:
#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.");
}
}
!= '\0'
經常被省略,因為\0
被認為是假的(因為為零),而其他一切都被認為是真。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.