簡體   English   中英

如果我強制在長度為10的字符串中的第一個和最后一個為字母,如何強制用戶僅在字符串中插入數字? 示例S2351566A

[英]How to force user to insert only numbers in a string if I force the first and the last to be letters in a 10 length string? Example S2351566A

Int char10];// length of string 
{
   if (!isalpha(str[0]) || !isalpha(str[8]))// force 1st and last array to be alphabet
    { 
        printf("invalid input\n"); exit(1); // invalid 
    }
}

如果只有第一個和最后一個元素必須是字符,那么您可以嘗試這樣做,但是如果您希望第一個和最后一個元素為char且所有中間部分為數字,則在第一個“ if”之后添加另一個if語句

布爾b = false;

while(b!= true){

在ch [10]中輸入;

if(ch [0]> ='A'&& ch [0] <='Z'&& ch [9]> ='A'&& ch [0] <='9'){

//表示第一個和最后一個是char

b = true; //將因此而中斷}

其他{

打印f(“輸入錯誤,再次輸入”);

b = false;}

} //這將一直運行,直到用戶沒有提供正確的輸入

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM