當前位置:首頁 » 編程語言 » C語言如何判斷空格ns
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

C語言如何判斷空格ns

發布時間: 2023-01-20 07:53:20

c語言如何定義一個函數判斷一個字元串是否含有空白字元

這樣定義:
int haveaspace(char *s)
{
int i;
for(i=0;s[i];i++)
if(isspace(s[i]))
return 1;
return 0;
}
這是一個使用這個函數的一個例子:
#include <stdio.h>
#include <ctype.h>
int haveaspace(char *s)
{
int i;
for(i=0;s[i];i++)
if(isspace(s[i]))
return 1;
return 0;
}
int main()
{
char s1[]="aaa1A+";
char s2[]="aaa1 1A+";
if(haveaspace(s1))
printf("%s包含空白字元\n",s1);
else
printf("%s不包含空白字元\n",s1);
if(haveaspace(s2))
printf("%s包含空白字元\n",s2);
else
printf("%s不包含空白字元\n",s2);
return 0;
}

㈡ c語言怎麼判斷輸入的字元串是空格

#include<stdio.h>
#include<string.h>
struct info
{
char name[20];
char tel[20];
} person[4]; //一般定義結構體最好放到main函數的外面,這樣結構清晰
int main()//最好把void換為int,不過你那樣也對,這只是我的習慣
{

int i,j;
char findname[20];
char empty[20]=" ";
char a[20]="break";//我其實沒理解你這是干什麼的,反正應該不是你的問題
printf("please input a list\n");
for(i=0; i<4; i++)
scanf("%s %s",person[i].name,person[i].tel);//去掉&,字元串不需要,這里你最好再看一下字元串,
while(strcmp(findname,a)!=0)
{
printf("\nPlease input a name\n");
gets(findname);//scanf 會自動跳過空格,所以換成gets
for(i=0; i<4; i++)
{
if(strcmp(findname,person[i].name)==0)
{
printf("%s\n",person[i].tel);
break;
}
if(strcmp(findname,empty)==0)
{
for(j=0; j<4; j++)
printf("%s %s\n",person[j].name,person[j].tel);
break;
}
}

}
return 0;
}

㈢ c語言怎樣判斷一個字元串全為空格

你好!
不管是多少個空格的字元串,使用trim後,結果是空串。
你可以自己試驗下,string st = 「 ";st = st.trim();

㈣ 用C語言怎麼判斷一個字元串是否為空

空格和字元串為空的概念本來就不一樣,
如樓上所說空格本來就代表一個字元,
而空字元串直接為"\0"
如果你要把空格的字元串也定義為空字元串的話
就要加判斷
空格的ASCII碼為32
你只需要再多加一個遍歷判斷就OK

int IsEmpty = 0;
int length = strlen(變數);
int n;
//遍歷整個字元串,如果全為空格,則返回字元串也為空
for(n=0;n<length;n++)
{
//判斷ASCII碼是否為32
if(變數[n]==32)
IsEmpty=1;
else
{
IsEmpty=0;
//存在一個字元則跳出循環
break;
}
}
if(length == 0)
printf("字元串為空");
else if(IsEmpty)
printf("字元串為空");
else
printf("字元串不為空");

㈤ C語言輸出當中有沒有空格符號 怎麼確定啊

1、可以通過判斷輸出字元當中是否有空格字元來確定是否有空格符號。

2、空格符是存在的字元,ASCII是32。

例如:

chars[10000];//字元數組緩存
sprintf(s,"%d%c%f",.....);//先把輸出的內容先列印到字元數組緩存當中。
for(i=0;i<strlen(s);i++)if(s[i]==32)printf("有空格符號! ");//檢查字元串緩存中是否有空格字元。

㈥ 用C語言怎麼判斷一個字元串是否為空

空格不算是空字元串,str=""這樣的才算是空字元串,裡面什麼都沒有,而str=" "是有內容了,這個str存儲了一個字元(空格),如果你要把空格也算是空字元串,那麼要稍作修改:
char str[] = " ";
int len = strlen(str), i = 0;
if (len > 0)
{
while(i < len && str[i++] == ' ');
if (i < len) // 字元串不空
else // 空字元串
}

㈦ C語言用if和switch語句判斷字元是數字還是字母或者是空格,怎麼寫

if(character == ' '){
to do
}
else if((character >= ' a' && character <= 'z') ||(character >= ' A' && character <= 'Z' )){
to do
}

else if(character >= ' 0' && character <= ' 9'){
to do
}

else
to do