首页 > 其他分享 >实验五

实验五

时间:2023-12-03 23:46:04浏览次数:24  
标签:name int s1 char 实验 str printf

#include <stdio.h>
#define N 80
void replace(char *str, char old_char, char new_char);
int main() {
char text[N] = "c programming is difficult or not, it is a question.";
printf("原始文本: \n");
printf("%s\n", text);
replace(text, 'i', '*');
printf("处理后文本: \n");
printf("%s\n", text);
return 0;
}

void replace(char *str, char old_char, char new_char) {
int i;
while(*str) {
if(*str == old_char)
*str = new_char;
str++;
}
}

#include <stdio.h>
#include <string.h>
void sort(char *name[], int n);
int main() {
char *course[4] = {"C Program",
"C++ Object Oriented Program",
"Operating System",
"Data Structure and Algorithms"};
int i;
sort(course, 4);
for (i = 0; i < 4; i++)
printf("%s\n", course[i]);
return 0;
}
void sort(char *name[], int n) {
int i, j;
char *tmp;
for (i = 0; i < n - 1; ++i)
for (j = 0; j < n - 1 - i; ++j)
if (strcmp(name[j], name[j + 1]) > 0) {
tmp = name[j];
name[j] = name[j + 1];
name[j + 1] = tmp;
}
}
#include <stdio.h>
#include <string.h>
void sort(char *name[], int n);
int main() {
char *course[4] = {"C Program",
"C++ Object Oriented Program",
"Operating System",
"Data Structure and Algorithms"};
int i;
sort(course, 4);
for (i = 0; i < 4; i++)
printf("%s\n", course[i]);
return 0;
}
void sort(char *name[], int n) {
int i, j, k;
char *tmp;
for (i = 0; i < n - 1; i++) {
k = i;
for (j = i + 1; j < n; j++)
if (strcmp(name[j], name[k]) < 0)
k = j;
if (k != i) {
tmp = name[i];
name[i] = name[k];
name[k] = tmp;
}
}
}

#include <stdio.h>
#include <string.h>
#define N 5
int check_id(char* str);
int main()
{
    char* pid[N] = { 
    "31010120000721656X",
    "330106199609203301",
    "53010220051126571",
    "510104199211197977",
    "53010220051126133Y" };
    int i;
    for (i = 0; i < N; ++i)
        if (check_id(pid[i]))
            printf("%s\tTrue\n", pid[i]);
        else
            printf("%s\tFalse\n", pid[i]);
    return 0;
}
int check_id(char* str) {
    int judge = 1;
    int i;
    for (i = 0; i < 18; i++) {
        if (strlen(str) != 18) {
            judge = 0;
            break;
        }
        if (*(str + i) < 48 || *(str + i) > 57) {
            if (*(str + i) != 'X') {
                judge = 0;
                break;
            }
        }
    }
    
    return judge;
}

#include <stdio.h>
#define N 80
void encoder(char* str); // 函数声明
void decoder(char* str); // 函数声明
int main() {
    char words[N];
    printf("输入英文文本: ");
    gets(words);
    printf("编码后的英文文本: ");
    encoder(words); // 函数调用
    printf("%s\n", words);
    printf("对编码后的英文文本解码: ");
    decoder(words); // 函数调用
    printf("%s\n", words);
    return 0;
}

void encoder(char* str) {
    for (int i = 0; i < N; i++) {
        if (*(str + i) >= 'a' && *(str + i) <= 'z') {
            if (*(str + i) == 'z') {
                *(str + i) = 'a';
            }
            else {
                (*(str + i))++;
            }
        }
        if (*(str + i) >= 'A' && *(str + i) <= 'Z') {
            if (*(str + i) == 'Z') {
                *(str + i) = 'A';
            }
            else {
                (*(str + i))++;
            }
        }
    }
}

void decoder(char* str) {
    for (int i = 0; i < N; i++) {
        if (*(str + i) >= 'a' && *(str + i) <= 'z') {
            if (*(str + i) == 'a') {
                *(str + i) = 'z';
            }
            else {
                (*(str + i))--;
            }
        }
        if (*(str + i) >= 'A' && *(str + i) <= 'Z') {
            if (*(str + i) == 'A') {
                *(str + i) = 'Z';
            }
            else {
                (*(str + i))--;
            }
        }
    }
}

#include <stdio.h>

void sort(char* name[], int n);

int main(int argc, char* argv[]) {
    int i;
    sort(argv, argc);
    for (i = 1; i < argc; ++i) {
        printf("hello, %s\n", argv[i]);
    }
    return 0;
}

void sort(char* name[], int n) {
    int i, j;
    char* tmp;
    for (i = 0; i < n - 1; ++i)
        for (j = 0; j < n - 1 - i; ++j)
            if (strcmp(name[j], name[j + 1]) > 0) {
                tmp = name[j];
                name[j] = name[j + 1];
                name[j + 1] = tmp;
            }
}

 


#include<stdio.h> #include<string.h> #define N 80 int main(){ char s1[]="Learning makes me happy"; char s2[]="Learning makes me sleepy"; char tmp[N]; printf("sizeof(s1) vs. strlen(s1):\n"); printf("sizeof(s1) = %d\n", sizeof(s1)); printf("strlen(s1) = %d\n", strlen(s1)); printf("/nbefore swap:\n"); printf("s1:%s\n",s1); printf("s2:%s\n",s2); printf("\nswapping...\n"); strcpy(tmp,s1); strcpy(s1,s2); strcpy(s2,tmp); printf("\nafter swap:\n"); printf("s1:%s\n",s1); printf("s2:%s\n",s2); return 0; }

 

#include<stdio.h>
#include<string.h>
#define N 80

int main(){
    char *s1="Learning makes me happy";
    char *s2="Learning makes me sleepy";
    char *tmp;
    
    printf("sizeof(s1) vs. strlen(s1):\n");
    printf("sizeof(s1) = %d\n", sizeof(s1));
    printf("strlen(s1) = %d\n", strlen(s1));

    printf("/nbefore swap:\n");
    printf("s1:%s\n",s1);
    printf("s2:%s\n",s2);

    printf("\nswapping...\n");
    tmp=s1;
    s1=s2;
    s2=tmp;

    printf("\nafter swap:\n");
    printf("s1:%s\n",s1);
    printf("s2:%s\n",s2);

    return 0;
}

 

#include <stdio.h>
#include <stdio.h>
int main() {
int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
int i, j;
int *ptr1;
int(*ptr2)[4];
printf("输出1: 使用数组名、下标直接访问二维数组元素\n");
for (i = 0; i < 2; ++i) {
for (j = 0; j < 4; ++j)
printf("%d ", x[i][j]);
printf("\n");
} 
printf("\n输出2: 使用指向元素的指针变量p间接访问二维数组元素\n");
for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) {
printf("%d ", *ptr1);
if ((i + 1) % 4 == 0)
printf("\n");
} 
printf("\n输出3: 使用指向一维数组的指针变量q间接访问二维数组元素\n");
for (ptr2 = x; ptr2 < x + 2; ++ptr2) {
for (j = 0; j < 4; ++j)
printf("%d ", *(*ptr2 + j));
printf("\n");
} 
return 0;
}

 

标签:name,int,s1,char,实验,str,printf
From: https://www.cnblogs.com/u329089/p/17859837.html

相关文章

  • 实验五
    task1_1源码:#include<stdio.h>#defineN5voidinput(intx[],intn);voidoutput(intx[],intn);voidfind_min_max(intx[],intn,int*pmin,int*pmax);intmain(){inta[N];intmin,max;printf("录入%d个数据:\n",N);......
  • 实验五 继承和多态
    task3machinepets.hpp#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststrings);MachinePets();stringget_nickname()const;public:virtualstringtalk(......
  • 实验5 继承和多态
    实验任务1源代码:#pragmaonce#include<iostream>#include<string>usingstd::cout;usingstd::endl;usingstd::string;//发行/出版物类:Publisher(抽象类)classPublisher{public:Publisher(conststring&s="");//构造函数......
  • 实验5
    实验任务31#pragmaonce2#include<string>3#include<iostream>45classMachinePets{6public:7MachinePets(conststd::string&s);8virtual~MachinePets()=default;910conststd::string&get_nickname()c......
  • 实验五
    实验1  功能是分别找到输入数中的最小值和最大值两者都指向x[0]功能是找到最大数,返回的是最大数的地址可以,因为改变之后返回的也是最大值的地址 实验2  s1大小是24个字节,sizeof以字节为单位返回一个变量,表达式或类型的长度,strlen是计算字符串长度的,不包含结尾......
  • 实验五 继承和多态
    Task3:pets.hpp:#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststrings):nickname(s){}conststringget_nickname(){returnnickname;}virtualstringta......
  • 实验5
    实验任务1:1.1:  1.功能:寻找到输入的多个数据中最大的和最小的数据2.指向该数组中的第一项 1.2:  1.返回的是该数组中最大数据的地址2.不行,算法表达不正确实验任务2:2.1: 问题1: 大小:24 计算:数组s1的大小统计:数组的数据字符串的长度 问题2:不行表达......
  • 实验5 继承和多态
    实验任务3pets.hpp #pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{private:stringnickname;public:MachinePets(conststrings):nickname{s}{}stringget_nickname()const{returnnicknam......
  • 实验五 继承和多态
    任务三pets.hpp#pragmaonce#include<iostream>#include<string>usingnamespacestd;classMachinePets{public:MachinePets(conststring&nickname="");stringgetNickname()const;virtualstringtalk()=0;prot......