2014年8月4日 星期一

(綠色皮) 最新C程式語言, 施威銘研究室 著, 旗標 第十四章 '參考' 答案

==============================================
/* 程式檔名:test_14-30-1*/
/* 程式目的:將字串變成 I am a good student*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400802*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    char str[]="I $%am 5a%$ good#@$ student";
    int i;
    int lon=strlen(str);
    for(i=0;i<lon;i++)
    {
        
         if(isalpha(str[i])!=0 || isspace(str[i])!=0)
         {
          printf("%c",str[i]);
         }
    }
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_14-30-2*/
/* 程式目的:數字 字母 的總和*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400802*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    char str[]="dlp93ub4u190f4ul";
    int i;
    int sum=0,sum1=0;
    int lon=strlen(str);
    for(i=0;i<lon;i++)
    {     
         if(isalpha(str[i])==0)
         {
          sum=sum+str[i];
         }
         if(isalpha(str[i])!=0)
         {
          sum1=sum1+str[i];
         }
    }
    printf("數字總合:%d\n",sum);
    printf("英文總合:%d\n",sum1);
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_14-30-3*/
/* 程式目的:數字 字母 分開 螢幕輸出*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400802*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    char str[]="dlp93ub4u190f4ul";
    int i;
    int lon=strlen(str);
    for(i=0;i<lon;i++)
    {
        
         if(isalpha(str[i])!=0)
         {
          printf("%c",str[i]);
         }

    }
    printf("\n");
    for(i=0;i<lon;i++)
    {
        
         if(isalpha(str[i])==0)
         {
          printf("%c",str[i]);
         }
    }
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_14-30-4*/
/* 程式目的:將字串變成 fIewRdWdfipwW 大小寫字母分開輸出*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400802*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    char str[]="fIewRdWdfipwW";
    char str1[]="";
    int i;
    int lon=strlen(str);
    for(i=0;i<lon;i++)
    {
        
         if(isupper(str[i])!=0)
         {
          printf("%c",str[i]);
         }
    }
    printf("\n");
    for(i=0;i<lon;i++)
    {
        
         if(isupper(str[i])==0)
         {
          printf("%c",str[i]);
         }
    }
    printf("\n");
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_14-30-5*/
/* 程式目的:將字串變成 I am a good student*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400802*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    char str[]="I $%am 5a%$ good#@$ student";
    int i;
    int lon=strlen(str);
    for(i=0;i<lon;i++)
    {
        
         if(isalpha(str[i])!=0)
         {
          printf("%c",str[i]);
         }
    }
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_14-30-6*/
/* 程式目的:計算輸入字串中的標點符號數目*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400802*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    char *str="ij32\aojp\v\ndoe\t";
    int i;
    int punct=0;
    printf("第 ");
    for(i=0;i<strlen(str);i++)
    {
        if(isalnum(*(str+i))==0)
        {
         punct=punct+1;
         printf("%d ",i+1);
        }
    
    }
    printf("為特殊字元的位置\n");
    printf("特殊與標點符號 %d 個\n",punct);
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}

void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_14-30-7*/
/* 程式目的:將字串中的標點符號除掉*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400802*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    char string[]="Book, knowledge sotrage,is a useful tool.";
    int i;
    int count=0;
    
    for(i=0;i<strlen(string);i++)
    {
     if(ispunct(string[i])==0)
     {
      printf("%c",string[i]);
     }
    }
    printf("您輸入的字串中,含有 %d 個標點符號\n",count);
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_14-30-8*/
/* 程式目的:螢幕輸出有多少小寫字母 大寫字母 數字 及 其他符號*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400802*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    char str[100];
    int i,lon;
    int upcase=0,lowcase=0,punct=0,space=0;
    
    printf("請輸入字串\n");
    gets(str);
    lon=strlen(str);
    for(i=0;i<lon;i++)
    {
        if(isupper(*(str+i))!=0)
        {
         upcase=upcase+1;
        }
        else if(islower(*(str+i))!=0)
        {
         lowcase=lowcase+1;
        }
        else if(isdigit(*(str+i))!=0)
        {
         punct=punct+1;
        }
        else if(ispunct(*(str+i))!=0)
        {
         space=space+1;
        }
    
    }
    
    printf("大寫字母 %d 個\n",upcase);
    printf("小寫字母 %d 個\n",lowcase);
    printf("數字 %d 個\n",punct);
    printf("特殊與標點符號 %d 個\n",space);
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}

==============================================
/* 程式檔名:test_14-30-9*/
/* 程式目的:輸入 I am a good student 輸出 I am a good student*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400802*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    char name[]="I am a good student";
    int i,flag;
    
    do
    {
        flag=1;
        for(i=0;i<strlen(name);i++)
        {
         if(isalpha(name[i])==0 && isspace(name[i])==0)
         {
          flag=0;
          printf("含有非英文字母的字元,請重新輸入\n");
          break;
         }
        }
    }while(flag==0);
    
    printf("您的英文名子是 %s \n",name);

    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}
==============================================
/* 程式檔名:test_14-30-10*/
/* 程式目的:只能輸入英文字串與空白 從螢幕輸出該字串第一個字母必須是大寫 其他為小寫*/
/* 程式設計:蘇彥儒*/
/* 完成日期:201400802*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

void Pause(const char* message);

int main(int argc, char *argv[])
{
    char ID[100];
    unsigned int i;
    int flag,lon;
    do
    {
        printf("請設定您的 ID \n");
        printf("(限定為 16 個字元以內的英文字母或數字)\n");
        gets(ID);
        lon=strlen(ID);
        flag=1;
        for(i=0;i<lon;i++)
        {
         if(isalpha(ID[i])==0 && isspace(ID[i])==0)
         {
          printf("只能輸入英文字串與空白\n\n");
          flag=0;
          break;
         }
         if(islower(ID[0])!=0)
         {
           ID[0]=ID[0]-32;
         }
         else if(isupper(ID[i+1])!=0)
         {
           ID[i+1]=ID[i+1]+32;
         }
        }
    }while(flag!=1);
    
    printf("您設定的ID: %s\n",ID);
    
    Pause("按 ENTER 繼續"); 
    return EXIT_SUCCESS;
}


void Pause(const char* message)
{
    printf("%s \n", message);
    rewind(stdin);
    getchar(); 

}
==============================================

沒有留言:

張貼留言