C Primer Plus 第十一章 编程练习 1-15题

第1题
#include<stdio.h>
void readChar(char* words , int len);
int main(void)
{
  int LEN;
  printf("Please Input The Max Lenght:\n");
  scanf("%d",&LEN);
  getchar();
  char Letters[LEN];
  readChar(Letters,LEN);
  for(int i = 0 ; i < LEN ; ++i)
    printf("%c ",*(Letters+i));

 return 0;
}
void readChar(char* words , int len)
{
  printf("Please Input %d Chars.",len);
  fgets(words,len+1,stdin);
}


第3题
#include<stdio.h>
const char stop_1 = ' ';
const char stop_2 = '\t';
const char stop_3 = '\n';
const int LEN = 100;
int getWord(char* words , int Len , char* firstWord);
int main(void)
{
  char getWords[LEN];
  char FWord[LEN];
  printf("Please Input:\n");
  fgets(getWords,LEN,stdin);
  int realLen = getWord(getWords,LEN,FWord);
  printf("First Word Is :");
  for(int i = 0 ; i < realLen ; ++i)
    printf("%c",*(FWord+i));

 return 0;
}
  
int getWord(char* words , int Len , char* firstWord)
{
  char mid;
  int i = 0;
  for( ; i < Len ; ++i)  //  *-*
  {
    mid = *(words+i);
    if(mid == stop_1 || mid == stop_2 || mid == stop_3)
    {
      *(firstWord+i) = '\0';
      break;
    }
    else 
      *(firstWord+i) = mid;
  }
  return i;
}  


第4题
#include<stdio.h>
const int MLen = 100;
const char unKnowChar = '\0';
const char* findChar(const char* sourceWord , int Len ,char targetChar);
int main(void)
{
  const char tarChar = 'a'; // the target char is 'a' 
  char TestWord[MLen];
  printf("Please Input Any Words:");
  fgets(TestWord,MLen,stdin);
  const char* FChar = findChar(TestWord,MLen,tarChar);
  if(FChar == &unKnowChar)
  {
    printf("We Can't Find 'a' on This Word.");
    return 0;
  }
  for(int i = 0 ; i < MLen ; ++i)
  {
    if(FChar == (TestWord+i))
    printf("We Find Letter That's 'a' On %dth.",i+1);
  }

 return 0;
}
const char* findChar(const char* sourceWord , int Len ,char targetChar)
{
  for(int i = 0 ; i < Len ; ++i)
  {
    if(*(sourceWord+i) == targetChar)
      return sourceWord+i;
  }
  return &unKnowChar;
}


第5题
#include<stdio.h>
#include<stdbool.h>
const char MLen = 100;
bool findChar(const char* source , char tarChar);
int main(void)
{
  const char tarChar = 'a';
  char words[MLen];
  printf("Please Input Ant Words:");
  fgets(words,MLen,stdin);
  bool result = findChar(words,tarChar);
  if(result == true)
    printf("We Find THe Char On This Word.\n");
  else 
    printf("We Can't Find.\n");

 return 0;
}
bool findChar(const char* source , char tarChar)
{
  for(int i = 0 ; ; ++i)
  {
    if(*(source+i) == tarChar)
      return true;
    if(*(source+i) == '\0')
      break;
  }
 
  return false;
}


第6题
#include<stdio.h>
const int MLen = 100;
const int copyLen = 10;
char* myCopy(char* tar , const char* source , int Len);
int main(void)
{
  char cinWord[MLen];
  char tarWord[copyLen];
  printf("Please Intpu Any Words:");
  fgets(cinWord,MLen,stdin);
  myCopy(tarWord,cinWord,copyLen);
  for(int i = 0 ; i < copyLen ; ++i)
  {
    if(*(tarWord+i) == '\0')
      printf("*");
    printf("%c",*(tarWord+i));
  }
 
 return 0;
}    
char* myCopy(char* tar , const char* source , int Len)
{
  for(int i = 0 ; i < Len ; ++i)
    *(tar+i) = *(source+i);
  return tar;
}


第7题
#include<stdio.h>
#include<stdbool.h>
const int MLen = 100;
const char None = '\0';
const char* string_in(const char* source , const char* tar);
int main(void)
{
  const char tarChar[3] = {'a','t'};
  char cinWord[MLen];
  printf("Please Input Any Words To Test:");
  fgets(cinWord,MLen,stdin);
  const char* findChar = string_in(cinWord,tarChar);   
  if(findChar == &None)
  {
    printf("This Word Don't Include Words \"at\".");
    return 0;
  }

  int index;
  for(int i = 0 ; i < MLen ; ++i)
  {
    if(findChar == cinWord+i)    
    {
      index = i;
      break;
    }
  }
  printf("The TargetWord begin %dth Letters.\n",index+1);

 return 0;
}  
const char* string_in(const char* source , const char* tar)
{
  char FChar = *tar;
  int beginIndex = 0;
  bool result = true;
  for(int i = 0 ; ; ++i)   //find first char 
  {
    if(*(source+i) == '\0')
      break;
    if(*(source+i) == FChar)
      beginIndex = i;
  }
  if(beginIndex == 0)  //Don't Find Word
    result = false;
  for(int i = 0 ; *(tar+i) != '\0' ; ++i)
  {
    if((*(source+beginIndex+i)) != (*(tar+i)))
    {
      result = false;
      break;
    }
  }
  
  if(result == true)
    return source+beginIndex;
  else
    return &None;
}


第8题
#include<stdio.h>
#include<string.h>
const int MLen = 100;
void turnChar(char* word , int Len);
int main(void)
{
  char cinWord[MLen];
  printf("PLease Input Any Word To Test:");
  fgets(cinWord,MLen,stdin);
  int realLen = strlen(cinWord);
  turnChar(cinWord,realLen);
  for(int i = 0 ; i < realLen ; ++i)
    printf("%c",*(cinWord+i));

 return 0;
}
void turnChar(char* word , int Len)
{
  char mid[Len];
  strncpy(mid,word,Len);
  int j = Len-1;
  for(int i = 0; i < Len ; ++i,--j)
    strcpy(word+i,mid+j);
}


第9题
#include<stdio.h>
#include<string.h>
const int MLen = 100;
void popSpace(char* source , int Len);
int main(void)
{
  char words[MLen];
  printf("Please Input Any Words To Test:");
  fgets(words,MLen,stdin);
  popSpace(words,MLen);
  fputs(words,stdout);
 
 return 0;
}
void popSpace(char* source , int Len)
{
  char mid[Len];
  strcpy(mid,source);
  for(int i = 0 , j = 0; i < Len ; ++i)
  {
    if(*(mid+i) == ' ')
      continue;
    else
    {
      strcpy(source+j,mid+i);
      ++j;
    }
  }
}


第10题
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
const int MLen = 100;
const int EleLen = 10;
void putASCII(char (*word)[MLen] , int n);
void putSource(char (*word)[MLen] , int n);
void putLenght(char (*word)[MLen] , int n);
void putFirstWord(char (*word)[MLen] , int n);
int LenForFirstWord(char* word , int n);  // Lenght For First Words
int main(void)
{
  char TestWord[EleLen][MLen];
  int realLen = 0;
  for(int i = 0 ; i < EleLen ; ++i)
  {
    printf("PLease INput %dth Words:",i+1);
    fgets(*(TestWord+i),MLen,stdin);
    ++realLen;
  }

  bool on_off = true;
  while(on_off)
  {
    printf("A:ouput B:by ASCII C: by Lenght\n");
    printf("D:by Lenght Of First Word Q:quit\n");
    char ch;
    scanf("%c",&ch);
    getchar();
    switch(ch)
    {
      case 'A':putSource(TestWord,realLen);
               break;
      case 'B':putASCII(TestWord,realLen);
               break;
      case 'C':putLenght(TestWord,realLen);
               break;
      case 'D':putFirstWord(TestWord,realLen);
               break;
      case 'Q':on_off = false;
               break;
      default:on_off = false;
              break;
    }
  }
  printf("Done!n");

 return 0;
}

void putASCII(char (*word)[MLen] , int n)
{
  for(int i = 0 ; i < n ; ++i)
  {
    for(int j = 0 ; j < MLen ; ++j) 
    {
      char ch = *(*(word+i)+j);
      if(ch == '\0')
        break;
      printf("%d ",ch);
    }
    printf("\n");
  }
}
void putSource(char (*word)[MLen] , int n)
{
  for(int i = 0 ; i < n ; ++i)
    fputs(*(word+i),stdout);
}
void putLenght(char (*word)[MLen] , int n)
{
  char mid[n][MLen];
  for(int i = 0 ; i < n ; ++i)
    strcpy(*(mid+i),*(word+i));
  for(int i = 0 ; i < n ; ++i)   //sort by lenght
  {
    for(int j = i ; j < n ; ++j)
    {
      char midChar[MLen];
      if(strlen(*(mid+i)) > strlen(*(mid+j)))
      {
        strcpy(midChar,*(mid+i));
        strcpy(*(mid+i),*(mid+j));
        strcpy(*(mid+j),midChar);
      }
    }
  }
  for(int i = 0 ; i < n ; ++i)
    fputs(*(mid+i),stdout);  
}
void putFirstWord(char (*word)[MLen] , int n)
{
  char mid[n][MLen];
  for(int i = 0 ; i < n ; ++i)
  {
    printf("%d\n",LenForFirstWord(*(word+i),MLen));
    printf("%d\n",LenForFirstWord(*(mid+i),MLen));
    strcpy(*(mid+i),*(word+i));
  }
  for(int i = 0 ; i < n ; ++i)   //sort by lenght
  {
    for(int j = i ; j < n ; ++j)
    {
      char midChar[MLen];
      if(LenForFirstWord(*(mid+i),MLen) > LenForFirstWord(*(mid+j),MLen))
      {
        strcpy(midChar,*(mid+i));
        strcpy(*(mid+i),*(mid+j));
        strcpy(*(mid+j),midChar);
      }
    }
  }
  for(int i = 0 ; i < n ; ++i)
    fputs(*(mid+i),stdout);  
}
int LenForFirstWord(char* word , int n)
{
  int Lenght = 0;
  for(int i = 0 ; i < n ; ++i)
  {
    if((*(word+i)) == ' ')
    {
      Lenght = i;  
      break;
    }
  }
  return Lenght;
}


第11题
#include<stdio.h>
#include<ctype.h>
const int MaxLen = 100;
int main(void)
{
  char Word[MaxLen];
  printf("Please Inupt Any Words To Test:\n");
  int Counts = 0;
  while(Counts < MaxLen-1)    //input
  {
    char ch = getchar();
    if(ch != EOF)
    {
      Word[Counts] = ch;
      ++Counts;
    }
    if(ch == EOF)
      break;
  }
  Word[Counts] = '\0';

  int Letters = 0;
  int upLetters = 0;
  int lowLetters = 0;
  int punct = 0;
  int digit = 0;
  for(int i = 0 ; i < Counts ; ++i)
  {
    if(isalpha(Word[i]))
      ++Letters;
    if(isupper(Word[i]))
      ++upLetters;
    if(islower(Word[i]))
      ++lowLetters;
    if(ispunct(Word[i]))
      ++punct;
    if(isdigit(Word[i]))
      ++digit;
  }

  printf("\nWe Get %d Letters,%d Upper,%d Lower,%d Punct,%d Digit.\n",Letters,upLetters,lowLetters,punct,digit);

 return 0;
} 


第12题
#include<stdio.h>
int main(int argc , char* argv[])
{
  if(argc == 1)
  {
    printf("Bye!\n");
    return 0;
  }
  for(int i = argc-1 ; i > 0 ; --i)
  {
    fputs(*(argv+i),stdout);
    printf(" ");
  }
 
 return 0;
}


第13题
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
int myAtoi(char* v);
int main(int argc , char* argv[])
{
  if(argc != 3)
  {
    printf("Wrong Element!\n");
    printf("Done!\n");
    return 0;
  }
  double D = atof(argv[1]);
  int E = myAtoi(argv[2]);
  double Pows = 1;
  for(int i = 0 ; i < E ; ++i)
    Pows *= D;
  
  printf("%.2lf\n",Pows);
  printf("Done!\n");

 return 0;
}
int myAtoi(char* v)
{
  int Index;
  for(int i = 0 ; ; ++i)
  {
    if(!isdigit(v[i]))
    {
      Index = i;
      break;
    }
  }

  int Value = 0;
  for(int i = Index-1,j=0 ; i >= 0 ; --i,++j)
  {
    int mid = v[i];
    Value += (mid-48)*pow(10,j);
  }
  printf("%d\n",Value);
  return Value;
}


第14题
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<math.h>
int main(int argc , char** argv)
{
  if(argc != 2)
  {
    printf("Wrong Element.");
    return 0;
  }
  int Len = 0;
  int allLen = strlen(*(argv+1));
  for(int i = 0 ; i < allLen ; ++i)
  {
    if(((*(argv+1))[i]) > 47 && (*(argv+1))[i] < 59)  
    Len += 1;
  }
  int Res = 0;
  for(int i = 0 ; i < Len ; ++i)
    Res += ((*(argv+1))[i] - 48)* pow(10,Len-1-i);  //on ASCII, char '0' = 48(int)
  
  printf("%d",Res);
  return 0;
}


第15题
#include<stdio.h>
#include<string.h>
#include<ctype.h>
const int MaxLen = 100;
const char choose_1[3] = "-p\0";
const char choose_2[3] = "-u\0";
const char choose_3[3] = "-l\0";
int main(int argc , char* argv[])
{
  if(argc != 2)
  {
    printf("Wrong Element!\n");
    printf("Done!\n");
    return 0;
  }

  char words[MaxLen];
  int Counts = 0;
  printf("Please Inupt Any Words To Test:\n");
  while(Counts < MaxLen-1)
  {
    char ch = getchar();
    if(ch != EOF)
    {
      words[Counts] = ch;
      ++Counts;
    }
    if(ch == EOF)
      break;
  }
  words[Counts] = '\0';
  printf("\n");

  if(strncmp(*(argv+1),choose_1,2) == 0)
  {
    fputs(words,stdout);
    return 0;
  }

  if(strncmp(*(argv+1),choose_2,2) == 0)
  {
    for(int i = 0 ; i <= Counts ; ++i)
    {
      if(islower(words[i]))
        words[i] = toupper(words[i]);
    }
    fputs(words,stdout);
    return 0;
  }

  if(strncmp(argv[1],choose_3,2) == 0)
  {
    for(int i = 0 ; i <= Counts ; ++i)
    {
      if(isupper(words[i]))
        words[i] = tolower(words[i]);
    }
    fputs(words,stdout);
    return 0;
  }
}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值