1.
#include
#define SIZE 40
void s_gets(char str[], int n);
int main(void)
{
int i;
int num;
char st [SIZE];
printf("Enter a number for the length of the string:");
scanf("%d",&num);
while(getchar() != '\n')
continue;
s_gets(st,num);
fputs(st,stdout);
printf("\n");
for (i = 0; i < num; ++i) {
printf("%d\n",st[i]);
}
return 0;
}
void s_gets(char str[], int n)
{
printf("Enter a string:");
fgets(str,n,stdin);
}
2.
#include
#include
#define SIZE 40
void s_gets(char str[], int n);
int main(void)
{
int i;
int num;
char st [SIZE];
printf("Enter a number for the length of the string:");
scanf("%d",&num);
while(getchar() != '\n')
continue;
s_gets(st,num);
puts(st);
printf("\n");
for (i = 0; i < num; i++) {
printf("%d\n",st[i]);
}
return 0;
}
void s_gets(char str[], int n)
{
int i;
printf("Enter a string:");
for (i = 0; i < n; i++) {
str[i] = getchar();
if(isspace(str[i])) {
str[i] = '\0';
break;
}
}
}
3.
#include
#include
#include
#define SIZE 80
void s_gets(char * st);
int main(void)
{
char str [SIZE];
int length;
int i;
printf("Enter a string, we will get the first word:");
s_gets(str);
length = strlen(str);
for(i = 0; i < length; i++)
printf("%c",str[i]);
return 0;
}
void s_gets(char * st)
{
int i = 0;
int j;
while(isspace(st[i] = getchar()))
continue;
for(j = 1; ; j++)
{
st[j] = getchar();
if(isspace(st[j]))
{
st[j] = '\0';
break;
}
}
}
4.
#include
#include
#include
#define SIZE 80
void s_gets(char * st, int n);
int main(void)
{
char str [SIZE];
int length;
int i,max;
printf("Enter the up limit of the letters in the word:");
scanf("%d",&max);
printf("Enter a string, we will get the first word:");
s_gets(str,max);
length = strlen(str);
for(i = 0; i < length; i++)
printf("%c",str[i]);
return 0;
}
void s_gets(char * st, int n)
{
int i = 0;
int j;
while(isspace(st[i] = getchar()))
continue;
for(j = 1; j < n; j++)
{
st[j] = getchar();
if(isspace(st[j]))
{
st[j] = '\0';
break;
}
}
}
5.
#include
#include
#define SIZE 80
char * select(char * st, const char ch);
int main(void)
{
char str[SIZE];
char target;
char * ans;
printf("Enter a srting(less than 80 letters):");
fgets(str,SIZE,stdin);
printf("Enter the character you want to find in the string:");
scanf("%c",&target);
ans = select(str,target);
if(ans == NULL)
printf("No character has been found.\n");
else
printf("The character is at the location of str[%d].",ans - str);
return 0;
}
char * select(char * st, const char ch)
{
char * ret;
int i,s;
s = strlen(st);
for(i = 0; i < s; i++)
{
if(st[i] == ch)
{
ret = &st[i];
return ret;
}
}
return NULL;
}
6.
#include
#include
#define SIZE 80
int is_within(char ch, char * st);
int main(void)
{
char str [SIZE];
char target;
int i;
printf("Enter the character you want to find in the string(# to quit):");
scanf("%c",&target);
getchar();
while(target != '#')
{
printf("Enter a srting(less than 80 letters):");
fgets(str,SIZE,stdin);
i = is_within(target, str);
if(i)
printf("We find the character in the string!\n");
else
printf("Nothing has been found.\n");
printf("Enter next character you want to find(# to quit):");
scanf("%c",&target);
getchar();
}
printf("bye!");
return 0;
}
int is_within(char ch, char * st)
{
int ret;
if(strchr(st,ch))
ret = 1;
else
ret = 0;
return ret;
}
7.
#include
#include
#define SIZE_OF_SOU 100
#define SIZE_OF_COPY 40
#define SIZE 20
char * mystrncpy(char * s1, char * s2, int n);
int main(void)
{
char sou[SIZE_OF_SOU];
char copy[SIZE_OF_COPY];
int i,l;
i = 0;
printf("Enter the source string(press Enter to quit):");
gets(sou);
while(sou[0] != '\0')
{
printf("Enter the second string you want to copy to source:");
gets(copy);
printf("Enter the length of copy:");
scanf("%d",&l);
getchar();
mystrncpy(sou, copy, l);
printf("Now the source string is:\n");
puts(sou);
while(sou[i])
{
sou[i] = '\0';
i++;
}
printf("Enter the source string(press Enter to quit):");
gets(sou);
}
printf("bye\n");
return 0;
}
char * mystrncpy(char * s1, char * s2, int n)
{
int i,len;
len = strlen(s1);
for(i = 0; i < n; i++)
s1[len+i] = s2[i];
return s1;
}
8.
#include
#include
#define SIZE 80
char * string_in(char * s1, char * s2);
int main(void)
{
char sou[SIZE];
char in[SIZE];
char * start;
int i = 0;
printf("Enter the source string(press Enter to quit):");
gets(sou);
while(sou[0] != '\0')
{
printf("Enter the string that is in the source string:");
gets(in);
start = string_in(sou,in);
if(start)
printf("The string is in the source string beginning at sou[%d].\n",start - sou);
else
printf("No string has been found in the source string.\n");
while(sou[i])
{
sou[i] = '\0';
i++;
}
printf("Enter the source string(press Enter to quit):");
gets(sou);
}
printf("bye");
return 0;
}
char * string_in(char * s1, char * s2)
{
int i;
int len_1,len_2;
len_1 = strlen(s1);
len_2 = strlen(s2);
for(i = 0; i < (len_1 - 1 - len_2); i++)
{
if(strncmp(s1+i, s2, len_2) == 0)
{
return (s1+i);
}
}
return NULL;
}
9.
#include
#include
#define SIZE 80
char * sort(char * st);
int main(void)
{
char sou[SIZE];
int i;
printf("Enter a string, I will sort it for you(press enter to quit):");
gets(sou);
while(sou[0] != '\0')
{
sort(sou);
puts(sou);
while(sou[i]) //用来清空sou数组中的元素,从而判断循环条件
{
sou[i] = '\0';
i++;
}
printf("Enter a string, I will sort it for you(press enter to quit):");
gets(sou);
}
printf("bye\n");
return 0;
}
char * sort(char * st)
{
int i;
int len;
char temp;
len = strlen(st);
for(i = 0; i < len / 2; i++)
{
temp = st[i];
st[i] = st[len - i - 1];
st[len - i - 1] = temp;
}
return st;
}
10.
#include
#include
#include
#define SIZE 100
char * delblk(char * st);
int main(void)
{
char sou[SIZE];
int i = 0;
printf("Enter a string, I will delete the blanks for you(Press enter to quit):");
gets(sou);
while(sou[0] != '\0')
{
delblk(sou);
fputs(sou,stdout);
printf("\n");
while(sou[i] != '\0')
{
sou[i] = '\0';
i++;
}
printf("Enter a string, I will delete the blanks for you(Press enter to quit):");
gets(sou);
}
printf("bye\n");
return 0;
}
char * delblk(char * st)
{
int i,j;
int len,tlen;
char temp[SIZE];
i = j = 0;
len = strlen(st);
while(i < len)
{
if(isspace(st[i]))
{
i++;
continue;
}
else
{
temp[j] = st[i];
i++;
j++;
}
}
i = 0;
while(st[i] != '\0') //清空st数组,以便下面重新赋值
{
st[i] = '\0';
i++;
}
tlen = strlen(temp);
for(i = 0; i < tlen; i++)
st[i] = temp[i]; //如果上面没有清空st数组,那么在赋值的时候只有前一部分元素赋值,后面未赋值的元素任然存在,所以会输出有问题
return st; //上面一部分也可以不要,但是这里如果返回temp的话,会被提示local variable的错误,局部变量在这里用完后是会被释放的,所以重新赋值st,具体原因到后面学习。
}
11.
#include
#include
#include
#define SIZE 100
void print(char * st);
void print_asc(char * st);
void print_up(char * st);
void print_first_length(char * st);
char *s_gets(char * st, int n);
int main(void)
{
char source[SIZE];
char choice;
printf("请输入一串英文字符(不超过80个英文字符):");
s_gets(source,10);
printf("----------------------------------------\n");
printf("| 请选择你想做的事: |\n");
printf("| a.顺序打印字符 b.打印ASCII码 |\n");
printf("| c.长度递增打印 d.第一个单词长度打印|\n");
printf("| q.不玩了 |\n");
printf("----------------------------------------\n");
scanf("%c",&choice);
getchar();
while(choice != 'q')
{
switch(choice)
{
case 'a':
print(source);
break;
case 'b':
print_asc(source);
break;
case 'c':
print_up(source);
break;
case 'd':
print_first_length(source);
break;
default:
printf("请输入正确选项:");
scanf("%c",&choice);
getchar();
continue;
}
printf("----------------------------------------\n");
printf("| 请选择你想做的事: |\n");
printf("| a.顺序打印字符 b.打印ASCII码 |\n");
printf("| c.长度递增打印 d.第一个单词长度打印|\n");
printf("| q.不玩了 |\n");
printf("----------------------------------------\n");
scanf("%c",&choice);
getchar();
}
printf("再见");
return 0;
}
void print(char * st)
{
printf("你的字符串是:\n");
puts(st);
}
void print_asc(char * st)
{
int i;
int len;
len = strlen(st);
printf("下面将打印你所输入字符串的ASCII码:\n");
for(i = 0; i < len; i++)
printf("%d ",st[i]);
printf("\n");
}
void print_up(char * st)
{
int i,j;
int len;
len = strlen(st);
printf("下面将按递增顺序打印字符串字符:\n");
for(i = 0; i < len; i++)
{
for(j = 0; j <= i; j++)
printf("%c",st[j]);
printf("\n");
}
}
void print_first_length(char * st)
{
int i,j;
int len;
len = strlen(st);
printf("下面将根据你第一个单词的长度打印字符串:\n");
for(i = 0; i < len; i++)
{
if(isspace(st[i]))
continue;
else
{
for(j = i; j < len; j++)
if(isspace(st[j]))
{
st[j] = '\0';
break;
}
break;
}
}
puts(st);
}
char *s_gets(char * st, int n)
{
char * ret_val;
int i = 0;
ret_val = fgets(st,n,stdin);
if (ret_val)
{
while(st[i] != '\n' && st[i] != '\0')
i++;
if (st[i] == '\n')
st[i] = '\0';
else
while(getchar() != '\n');
}
return ret_val;
}
12.
#include
#include
#include
#define SIZE 100
int main(void)
{
char source[SIZE];
int top_words,low_words,words,puncs,nums;
int i;
int len;
top_words = low_words = words = puncs = nums = 0;
printf("Enter a string:");
fgets(source, SIZE, stdin);
len = strlen(source);
for(i = 0; i < len ; i++)
{
if(isalpha(source[i]))
{
if(source[i] >= 'a' && source[i] <= 'z')
low_words++;
else
top_words++;
words++;
}
else if(ispunct(source[i]))
puncs++;
else if(isdigit(source[i]))
nums++;
}
printf("There are totally %d words, including %d uppers and %d lowers, %d punctuals and %d numbers.\n",words,top_words,low_words,puncs,nums);
printf("That's over!");
return 0;
}
13.
#include
int main(int argc,char *argv[])
{
int i;
for(i = 1; i < argc; i++)
printf("%s ",argv[argc-i]);
return 0;
}
14.
#include
#include
int main(int argc, char ** argv)
{
double base;
int index;
double ans = 1;
int i;
base = atof(argv[1]);
index = atoi(argv[2]);
for(i = 0; i < index; i++)
ans *= base;
printf("The result is %lf",ans);
return 0;
}
15.
#include
#include
#include
#include
#define SIZE 100
double atoi(char * st);
int main(void)
{
char num[SIZE];
double retu;
printf("Enter a string including numbers:");
gets(num);
retu = atoi(num);
printf("%lf\n",retu);
return 0;
}
double atoi(char * st)
{
int i;
int len;
double result = 0;
len = strlen(st);
for(i = 0; i < len; i++)
{
if(isdigit(st[i]))
{
result += ((st[i]-'0')*pow(10,(len-i-1)));
}
else
return 0;
}
return result;
}
16.
#include
#include
#include
#define SIZE 80
int main(int argc, char **argv)
{
char source[SIZE];
int i;
int len;
printf("Enter a string:");
fgets(source, SIZE, stdin);
len = strlen(source);
if(strcmp(argv[1],"-u") == 0) //这个地方不能直接判断argv[1] == "-u",如果这样写,那么比较的是字符串的地址,而不是字符串本身
{
for(i = 0; i < len; i++)
source[i] = toupper(source[i]);
fputs(source,stdout);
}
else if(strcmp(argv[1],"-l") == 0)
{
for(i = 0; i < len; i++)
source[i] = tolower(source[i]);
fputs(source,stdout);
}
else
fputs(source,stdout);
return 0;
}