練習(xí)題
-
char * s_gets(char * st, int n)
{
char * ret_val;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (*st != '\n' && *st != '\0')
st++;
if (*st == '\n')
*st = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
設(shè)計(jì)并測(cè)試一個(gè)函數(shù)富雅,從輸入中獲取下n個(gè)字符(包括空白,制表符陨收、換行符)饭豹,把結(jié)果存儲(chǔ)在一個(gè)數(shù)組里,他的地址唄船體作為一個(gè)參數(shù)
#include <stdio.h>
#define SIZE 40
void * s_gets(char * st, int n);
int main(void)
{
char st[SIZE];
puts("請(qǐng)輸入:");
s_gets(st, SIZE);
puts(st);
return 0;
}
void * s_gets(char * st, int n)
{
fgets(st, n, stdin);
}
修改并編程練習(xí)1的函數(shù)畏吓,在n個(gè)字符后停止墨状,或在讀到第一個(gè)空白、制表符或換行符時(shí)停止菲饼,哪個(gè)先遇到哪個(gè)停止肾砂。不能只使用scanf()
#include <stdio.h>
void sgets(char * st, int n);
void main(void)
{
char str[100];
int n;
printf("請(qǐng)輸入字符數(shù)量:");
scanf("%d", &n);
while (getchar() != '\n')
continue;
printf("請(qǐng)輸入字符:");
sgets(str, n);
puts(str);
}
void sgets(char * st, int n)
{
int i;
for (i = 0; i < n; i++)
{
st[i] = getchar();
if (st[i] == '\n' || st[i] ==' ' || st[i] == '\t')
{
break;
}
}
st[i] = '\0';
}
3
#include <stdio.h>
#define SIZE 40
void sgets(char * st);
int main(void)
{
char st[SIZE];
printf("請(qǐng)輸入字符:");
sgets(st);
puts(st);
}
void sgets(char * st)
{
scanf("%s", st);
while (getchar() != '\n')
continue;
}
4
#include <stdio.h>
#include <ctype.h>
void sgets(char * st, int n);
void main(void)
{
char st[100];
int num;
printf("請(qǐng)輸入字符數(shù)量:");
scanf("%d", &num);
printf("請(qǐng)輸入字母:");
sgets(st, num);
puts(st);
}
void sgets(char * st, int n)
{
int i = 0;
while (iswspace(st[i] = getchar()))
{
continue;
}
for ( i = 1; i < n; i++)
{
st[i] = getchar();
if (iswspace(st[i]))
{
st[i] = '\0';
break;
}
}
while (getchar() != '\n')
continue;
}
5
#include <stdio.h>
#include <string.h>
#define SIZE 100
char *strchrr(char * st, char chr);
void main(void)
{
char st[SIZE];
char ch;
int i;
char * fin;
printf("請(qǐng)輸入要查找的字符串:");
fgets(st, SIZE, stdin);
printf("請(qǐng)輸入查找的字符:");
ch = getchar();
fin = strchrr(st, ch);
if (fin)
{
printf("找到了\n");
}
else
{
puts("沒找到");
}
}
char *strchrr(char * st, char chr)
{
int x, i;
x = strlen(st);
for ( i = 0; i < x; i++)
{
if (st[i] == chr)
{
return &st[i];
}
}
return NULL;
}
編寫一個(gè)名為is_within()的函數(shù),接受一個(gè)字符和一個(gè)指向字符串的指針作為兩個(gè)函數(shù)的形參宏悦。如果指定字符在字符串中镐确,該函數(shù)返回一個(gè)非零值(真),否則返回0.在一個(gè)完整的程序中測(cè)試該函數(shù)饼煞,使用一個(gè)循環(huán)給函數(shù)提供輸入值
#include <stdio.h>
#include <string.h>
#define SIZE 100
int find(char ch, char * st);
void s_gets(char * st, int n);
char get_ch(void);
void main(void)
{
char st[SIZE];
char ch;
int x;
printf("請(qǐng)輸入查找的字符串:");
s_gets(st, SIZE);
while ((ch = get_ch()) != 'q')
{
x = find(ch, st);
if (x)
{
puts("找到了");
}
else
puts("沒找到");
}
}
void s_gets(char * st, int n)
{
char * ret_val;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (*st != '\n' && *st != '\0')
st++;
if (*st == '\n')
*st = '\0';
else
while (getchar() != '\n')
continue;
}
}
int find(char ch, char * st)
{
int i, x;
x = strlen(st);
for ( i = 0; i < x; i++)
{
if (st[i] == ch)
return 1;
}
return 0;
}
char get_ch(void)
{
char ch;
printf("請(qǐng)輸入字符:");
ch = getchar();
while (getchar() != '\n')
continue;
return ch;
}
7
#include <stdio.h>
#include <string.h>
char * mystrncpy(char * st, char * source, int n);
void main(void)
{
char string[20];
char source[40];
printf("請(qǐng)輸入:");
fgets(source, 40, stdin);
mystrncpy(string, source, 20 - strlen(string));
puts(string);
}
char * mystrncpy(char * st, char * source, int n)
{
int i;
for ( i = 0; i < n; i++)
{
st[i] = source[i];
}
st[19] = '\0';
return &st[0];
}
8
#include <stdio.h>
#include <string.h>
char * find(char * st, char * source);
void main(void)
{
char st[40];
char ch[40];
char * p;
printf("請(qǐng)輸入字符串:");
fgets(st, 40, stdin);
printf("請(qǐng)輸入字符:");
fgets(ch, 40, stdin);
p = find(st, ch);
if (p)
{
printf("在第%d個(gè)位置", (p - st) + 1);
}
else
{
puts("沒有");
}
}
char * find(char * st, char * source)
{
int i, x;
x = strlen(st);
for ( i = 0; i < x; i++)
{
if (source[0] == st[i])
return &st[i];
}
return NULL;
}
9
#include <stdio.h>
#include <string.h>
#define SIZE 40
char * sggets(char * st, int n);
void stsrt(char * str);
void main(void)
{
char st[SIZE];
char * p;
printf("請(qǐng)輸入:");
while (sggets(st, SIZE) != NULL && st[0] != '\0')
{
p = st;
stsrt(p);
puts(p);
}
}
char * sggets(char * st, int n)
{
char * ret_val;
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (*st != '\n' && *st != '\0')
st++;
if (*st == '\n')
*st = '\0';
else
while (getchar() != '\n')
continue;
}
return ret_val;
}
void stsrt(char * str)
{
int x, i;
char temp;
x = strlen(str);
for (i = 0; i < x / 2; i++)
{
temp = str[i];
str[i] = str[x - i - 1];
str[x - i - 1] = temp;
}
}
10
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 10
void del(char * st);
char * s_gets(char * st, int n);
void main(void)
{
char str[SIZE];
while ( s_gets(str, SIZE) != NULL && str[0] != '\0')
{
del(str);
puts(str);
}
}
char * s_gets(char * st, int n)
{
char * ret_val;
printf("請(qǐng)輸入字符串:");
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (*st != '\n' && *st != '\0')
st++;
if (*st == '\n')
*st = '\0';
else
while (getchar() != '\n')
continue;
}
}
void del(char * st)
{
char linshi[SIZE];
int i, j, gross;
gross = strlen(st);
for ( i = 0, j = 0; i <= gross; i++)
{
if (!isspace(st[i]))
{
linshi[j] = st[i];
j++;
}
}
i = 0;
while (st[i] != '\0')
{
st[i] = '\0';
i++;
}
gross = strlen(linshi);
for ( i = 0; i < gross; i++)
{
st[i] = linshi[i];
}
}
11
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SIZE 11
#define SIGN "*********************************"
char * s_gets(char * st, int n);
char get_choice(void);
char get_first(void);
void main(void)
{
char str[SIZE];
char ch;
int i;
s_gets(str, SIZE);
while ((ch = get_choice()) != 'q')
{
switch (ch)
{
case 'a':
puts(str);
break;
case 'b':
for ( i = 0; i < strlen(str); i++)
printf("%d ", str[i]);
printf("\n");
break;
case 'c':
puts("xiangbudaoba");
break;
case 'd':
puts("womexie");
break;
default:
puts("error");
break;
}
}
}
char get_choice(void)
{
char ch;
printf("%s%s\n", SIGN, SIGN);
puts("a) 打印字符串列表 b) 以ASCII中的順序打印字符串");
puts("c) 按長(zhǎng)度遞增順序打印字符串 d) 按自負(fù)床中第一個(gè)單詞的長(zhǎng)度打印字符串");
puts("q) 退出");
ch = get_first();
while (ch < 'a' || ch > 'd' && ch != 'q')
{
printf("請(qǐng)輸入a, b, c, d, q:");
ch = get_first();
}
return ch;
}
char get_first(void)
{
char ch;
ch = getchar();
while (getchar() != '\n')
{
continue;
}
return ch;
}
char * s_gets(char * st, int n)
{
char * ret_val;
printf("請(qǐng)輸入字符串:");
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (*st != '\n' && *st != '\0')
st++;
if (*st == '\n')
*st = '\0';
else
while (getchar() != '\n')
continue;
}
}
13
#include <stdio.h>
int main(int argc, char const *argv[])
{
int count;
count = argc - 1;
for ( ; count > 0; count-- )
{
printf("%s ", argv[count]);
}
return 0;
}
15
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char * s_gets(char * st, int n);
void ToUpper(char * st);
void ToLow(char * st);
int main(int argc, char const *argv[])
{
char st[100];
s_gets(st, 100);
if (argc > 1)
{
if (strcmp(argv[1], "-p") == 0)
puts(st);
else if(strcmp(argv[1], "-u") == 0)
{
ToUpper(st);
puts(st);
}
else if(strcmp(argv[1], "-l") == 0)
{
ToLow(st);
puts(st);
}
}
else
puts(st);
return 0;
}
void ToUpper(char * st)
{
int i;
for ( i = 0; i < strlen(st); i++)
{
st[i] = toupper(st[i]);
}
}
void ToLow(char * st)
{
int i;
for ( i = 0; i < strlen(st); i++)
{
st[i] = tolower(st[i]);
}
}
char * s_gets(char * st, int n)
{
char * ret_val;
printf("請(qǐng)輸入字符串:");
ret_val = fgets(st, n, stdin);
if (ret_val)
{
while (*st != '\n' && *st != '\0')
st++;
if (*st == '\n')
*st = '\0';
else
while (getchar() != '\n')
continue;
}
}