導航:首頁 > 凈水問答 > c輸入字元數組過濾空格

c輸入字元數組過濾空格

發布時間:2020-12-20 01:03:52

❶ C語言中輸入字元串,裡面有空格,怎麼根據空格把字元串分開,並存在數組里

程序源碼如下:

#include<stdio.h>

#include<string.h>

intmain(void)

{

char str[1000];//定義一個字元串數組

char strnew[1000];//定義一個備用字元串數組

char m[]="";//定義空格變數

printf("請輸入一串字元:");//文字提示輸入字元串

gets(str);//輸入字元串

char*p=strtok(str,m);//取str與m的指針

printf("%s ",p); //輸出

p=strtok(NULL,m);

while(p) //遍歷輸出

{

printf("%s ",p); //輸出字元串

p=strtok(NULL,m); //指向下一個

}

}

程序輸出結果:


(1)c輸入字元數組過濾空格擴展閱讀:

C語言:輸入一個字元串放入數組里,刪除其中的空格

#include <stdio.h>

#include<string.h>

#define N 100

void main()

{

int i=0,j;

char c,str[N];

printf("輸入字元串str: ");

while((c=getchar())!=' ')

{

str[i]=c;//輸入字元串

i++;

}

str[i]='';

for(i=0;str[i]!='';i++)

{

if(str[i]==' ')

{

for(j=i+1;str[j]!='';j++)

{

str[j-1]=str[j];

}

str[j]='';

}

else continue;

}

str[i-2]='';

printf("去掉空格後的字元串為: ");

for(i=0;str[i]!='';i++)

printf("%c",str[i]);

printf(" ");

}

❷ C語言字元型數組輸入時不能用%c ,我輸入的都是單個字元,輸入時字元之間沒有空格, 為什麼

在程序段scanf("%d",&n);後面加上getchar();問題應該就可以解決

❸ c語言 將輸入的字元串按照空格分割

strtok函數

網頁鏈接

char str[] ="i love c love c";

const char * split = " ";
char * p;
p = strtok (str,split);
while(p!=NULL) {
printf ("%s ",p);
p = strtok(NULL,split);
}

這么循環

說下我的邏輯,不一定最優

先弄個結構體回struct裡面有一個答char*和一個int

再建個struct的數組

在每次循環對比之前獲得的struct數組中是否含有相同的字元串

有就計數器+1

沒有就在數組中為null的地方加上一個成員為這個字元串和計數為1的struct

循環結束時遍歷struct數組

好久沒寫C了,怕給你的代碼有bug,就寫思路把

❹ 如何輸入包含空格的一個字元數組

C語言中,可以用gets函數來接收輸入的字元串(包含空格)。
格式:gets(字元數組專名);

功能:gets函數用於將屬輸入的字元串內容存放到指定的字元數組中,輸入結尾的換行符'\n'被換成'\0'存儲在該數組中。

舉例說明如下:

char str[20]; // 定義一個字元數組,大小為20個位元組
gets(str); // 將用戶輸入的字元串(可以包含空格)存儲到字元數組str中,以回車結束輸入
printf("%s", str); // 輸出用戶輸入的字元串(包含空格)
註:使用gets函數時,需將頭文件#include<stdio.h>包含到源文件中。

❺ 大神,我剛剛學c語言,問個問題,輸入的字元數組里有空格,怎麼把空格去掉,而且輸入的時候怎麼用回車結束

#include<stdio.h>


void myGets(char *buff)

{

char ch;

int i=0;

while((ch=getchar())!=' ')

{

if(ch!=' ')

*(buff+i++)=ch;

}

*(buff+i)='';

}


void main()

{

char array[60];

myGets(array);

printf("%s ",array);

}

❻ C語言:輸入一個字元串放入數組里,刪除其中的空格

程序:源
#include <stdio.h>
#include<string.h>
#define N 100
void main()
{
int i=0,j;
char c,str[N];
printf("輸入字元串str:\n");
while((c=getchar())!='\n')
{
str[i]=c;//輸入字元串
i++;
}
str[i]='\0';
for(i=0;str[i]!='\0';i++)
{
if(str[i]==' ')
{
for(j=i+1;str[j]!='\0';j++)
{
str[j-1]=str[j];
}
str[j]='\0';
}
else continue;
}
str[i-2]='\0';
printf("去掉空格後的字元串為:\n");
for(i=0;str[i]!='\0';i++)
printf("%c",str[i]);
printf("\n");
}
運行結果:
輸入字元串str:
ing ing ing
去掉空格後的字元串為:
inginging
Press any key to continue

❼ 用scanf程序輸入字元數組,在輸入的數字之間輸入空格對於c語言用%c輸出會出現奇怪的數字

後面是亂碼,你抄沒有初始化字元數組
char a[5] = {};
scanf遇到空格回車都會停止操作,所以當你輸入q空格時就只輸進了q,空格後面的數據暫時還在輸入緩沖區里等待讀入
所以只有a[0]是正確的數據,後面因為沒有初始化,是分配時儲存在裡面的舊數據,也就是亂碼

❽ 如何輸入一個帶空格的二維字元數組(用C語言)

#include<stdio.h>
#define M 5 //行數
#define N 5 //列數
char carray[M][N];
void getArray()
{
char str[10];
int i,j;
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
{
scanf("%s", str);
carray[i][j] = str[0];
}
}
}

int main()
{
int i,j;
getArray();
printf("Matex: %d %d\n", M, N);
for(i=0; i<M; i++)
{
for(j=0; j<N; j++)
printf("%c ", carray[i][j]);
printf("\n");
}
}

❾ c語言輸出字元數組空格

優化後的代碼,如下:
#include<iostream>
usingnamespacestd;
intmain()
{
charstr[100],t;
cin.getline(str,sizeof(str));//C++讀一行字元,內允許輸入空格,遇回車結束
for(inti=0;str[i];i++)//不要到99,要判斷
{
容if(str[i]<'v'&&str[i]>='a')
t=(str[i]+5);
else if(str[i]<='z'&&str[i]>='v')
t=(str[i]-21);
else
t=str[i];
cout<<t;
}
cout<<endl;
return0;
}

閱讀全文

與c輸入字元數組過濾空格相關的資料

熱點內容
蒸餾簡介專業英語論文 瀏覽:276
車上濾芯怎麼分 瀏覽:162
水過濾系統濾芯怎麼換 瀏覽:363
朗詩德凈水設備質量及售後如何 瀏覽:230
學校生活污水處理成套設備哪裡有 瀏覽:59
廢水雨水污水要分開嗎 瀏覽:206
高陽哪有污水處理廠 瀏覽:872
去除便器水垢的方法 瀏覽:892
純凈水養龜怎麼養 瀏覽:391
海關edi 瀏覽:135
廢水乙二醇可生化性 瀏覽:104
沸石會和蒸餾原料發生反應嗎 瀏覽:127
江西凈水系統多少錢 瀏覽:653
為什麼要洗超濾裝置 瀏覽:170
反滲透教育班會記錄 瀏覽:124
環氧樹脂灌膠去除 瀏覽:817
反滲透式凈水機產生的廢水 瀏覽:125
發電廠的脫硫廢水 瀏覽:306
空調過濾網壞了咋辦 瀏覽:386
焦化廢水與生活污水的區別 瀏覽:840