Open ghostbody opened 9 years ago
The C programming language has a set of functions implementing operations on strings (character strings and byte strings) in its standard library. Various operations, such as copying, concatenation, tokenization and searching are supported. For character strings, the standard library uses the convention that strings are null-terminated: a string of n characters is represented as an arrays of an n + 1 elements, the last of which is a "NUL" character.
The only support for strings in the programming language proper is that the compiler translates quoted string constants into null-terminated strings.
From wikipedia
String is a very important thing when we are programming. All of the input and ouput are strings and any kind of data can be translated into a "String Form" include int, double and so on. In other words, an integer or a double number can be represented by a string simply.
Strings in C programming language is implemented by using an character array whose length is n+1(n represents the valid length of the string). So the operations on Strings in C can be treated as the operations for arrays. The following standard C library includes some basic Cstring operations.
This header file defines several functions to manipulate C strings and arrays.
memcpy Copy block of memory (function ) memmove Move block of memory (function ) strcpy Copy string (function )... strncpy Copy characters from string (function )
strcat Concatenate strings (function ) strncat Append characters from string (function )
memcmp Compare two blocks of memory (function ) strcmp Compare two strings (function ) strcoll Compare two strings using locale (function ) strncmp Compare characters of two strings (function ) strxfrm Transform string using locale (function )
memchr Locate character in block of memory (function ) strchr Locate first occurrence of character in string (function ) strcspn Get span until character in string (function ) strpbrk Locate characters in string (function ) strrchr Locate last occurrence of character in string (function ) strspn Get span of character set in string (function ) strstr Locate substring (function ) strtok Split string into tokens (function )
memset Fill block of memory (function ) strerror Get pointer to error message string (function ) strlen Get string length (function )
NULL Null pointer (macro )
size_t Unsigned integral type (type )
From cplusplus.com
For header file, we just write the declaration of the functions and constants. For source file, we implement the functions(function definitions) For example: func.h
#ifndef FUNC_H_INCLUDED
#define FUNC_H_INCLUDED
int plus(int a, int b);
#endif //FUNC_H_INCLUDED
func.c
#include "func.h"
int plus(int a, int b) {
return a+b;
}
This time your task is not to just use the functions simply. For this assignment, you will be given serveral function prototypes and your job is to implement the functions with the descriptions and smaples.
#ifndef STRING_H_INCLUDED
#define STRING_H_INCLUDED
/*
Notice all the samples are use for function logic, not exactly the grammar
*/
/*
function: myStrlen
@description: Calculate the length of the string
@input: const string s
@output: the length of the string
@sample input: "abcde\0abcde"
@sample output: 5
@Notice: Function myStrlen() show check whether the input array is NULL.
If so, you show return 0. Note that strlen() in string.h does not check NULL arrays.
*/
unsigned int myStrlen(const char s[]);
/*
function: myStrcpy
@description: copy one string to another
@input: const string source
@output: string destination
@sample input: "a" "abc"
@sample output: "abc"
@Notice: You can not assign the array directly which will cause a runtime error.
*/
char * myStrcpy(char destination[], const char source[]);
/*
function: myStrcat
@input: const string source
@output: string destination
@sample input: "abcde" "abc"
@sample output: "abcdeabc"
@Notice: You can not assign the array directly which will cause a runtime error.
*/
char * myStrcat(char destination[], const char source[]);
/*
function: myStrcmp
@input: const string1 and const string2
@output: if string1 is euqal to string2, output 0
if string1 is greater than string2 ouput an integer > 0
if string2 is greater than string1 output an integer < 0
@sample input: "abc" "abc"
@sample output: 0
*/
int myStrcmp(const char str1[], const char str2[]);
/*
function: Mystrfind
@input: cosnt string1 and const string2
@output: if str2 is a substring of str1, output the first index in str1
otherwise, ouput -1 which indicates can not find
@sample input: "abcde" "cde"
@sample output: 2
*/
int MyStrfind(const char str1[], const char str2[]);
/*
function: LeftRotateString
@input: string buff, an integer n which indicates the first n
@output: put the first n chars to the end of the string
@sample input: "abcdefg" 4
@sample output: "efgabcd"
*/
void LeftRotateString(char *buff, int n);
/*
function: myParseInt
@input: a const string
@output: an integer parse from a string
@sample input: "123"
@sample output: 123
@Notice: You should notice show extreme conditions such as:
1 null array input: the input is an array, the program will crash in the access to the null array,
so you need to judge whether the array is null before using the array.
2 The sign: integer not only contain numbers, and may is the positive integer expressed
starts with a '+' or '-', so if the first character is' - ',
to get the integer value is converted to a negative integer.
3 illegal characters: the input string may contain characters that are not numbers.
Therefore, whenever you encounter these illegal characters, the program should stop converting and return the current value.
4 integer overflow: the number of input is the string of the form of input,
so the input of a very long string will likely lead to overflow. You should set the return
value to MAX_INT or MIN_INT in the situation.
*/
int myParseInt(const char str[]);
/*
function: myStrcontain
@input: const string1 and const string2
@output: a boolean value. id all chars in str2 are in str1, output true
else output flase
@sample input1: "ABCD" "BAD"
@sample output1: 1
@sample input2: "ABCD" "BCE"
@sample output2: 0
*/
int myStrcontain(const char str1[], const char str2[]);
#endif // STRING_H_INCLUDED
1、You need to write a file myString.c for all the implements of the function prototypes.
2、In file myString.c you can not include
#include "myString.h"
#ifndef NULL
#define NULL 0
#endif // NULL
/*code*/
/*function definations*/
/*code end*/
3、You can not change the function prototypes. 4、Black box testing will be again applied this experiment. Please check your file names for three times. 5、Please do not add main function in your file, you can only add it when you are testing.
char * myStrcat(char destination[], const char source[]);
, the array destination indicates the result of the function, why does the function add a return value char *
? Why not void myStrcat(char destination[], const char source[]);
?(Microsoft interview question)include <stdio.h>
int main() {
char s1[100] = "abcdefg";
char * s2 = "abcdefg";
s1[0] = 'z';
s2[0] = 'z';
return 0;
}
What will happen when we run the program and why? What if we remove s2[0] = 'z';
?
LeftRotateString
or myStrcontain
by other algorithm? (optional question, Microsoft interview questions)
Hint: String Reverse can be applied for LeftRotateString and hash table can be used for myStrcontain.Just answer the questions in deep thinking part
1、回答deep thinking中的问题
2、写一下反馈,吐槽一下我们的教学工作,给一些实质性的建议
作业提交方式:作业由学委收好,再发到我邮箱.
地址: ftp://172.18.182.75/ 远程目录: Experiment/软件班/实验4 C String and Function 命名规范:13331314_叶嘉祺_EX4_v0.zip 注意v0代表你提交的版本号,第一次提交为v0,第二次为v1,以此类推 注意,要打包压缩程ZIP格式
注意,文件压缩包里不能出现文件夹,只能包含一层目录
作业缓交或补交:每缓交一天 -5% 总分
缓交 -35% 分数
12月5日 18:00
师兄,这周Sicily的1004. Matrix Multiplication两个矩阵相乘那题中,EOF,我在网上查了是End Of File的缩写,然后在实际操作运行中,好像只有在命令行中用文件输入的形式才能完成程序。 如下图我的文件输入是: 在命令行显示是: 程序在输出结果后就结束了。 然后,我的问题是,在Dev上运行的话应该怎么结束程序?如果直接打“EOF”为什么会出现死循环? 上图如果我按回车就会出现死循环: 对了,1004题我是按照提示来做的,然后Sicily上Accept了。
EOF是end of file的缩写,表示"文字流"(stream)的结尾。这里的"文字流",可以是文件(file),也可以是标准输入(stdin)。 EOF不是特殊字符,而是一个定义在头文件stdio.h的常量,一般等于-1。
除了表示文件结尾,EOF还可以表示标准输入的结尾。但是,标准输入与文件不一样,无法事先知道输入的长度,必须手动输入一个字符,表示到达EOF。
(来源百度百科)
之所以你输入EOF出现死循环是因为EOF输入的是字符串。而scanf("%d", ....) != EOF
是通过scanf函数的返回值来判断的,当scanf遇到文件末尾就会返回-1,表示输入失败。
可以对这个问题进行进一步了解。
printf("%d\n", EOF); //输出啥?
printf("%d\n", scanf("%d", ....)); // 输出啥?
大家注意问题
1、文件命名绝对不能错 2、你需要先学习如何写一个函数库。也就是第一个任务,如何编写.h文件和.c文件分开,然后在main函数中调用.h文件中的函数。说白了就是明白#include这种实现的机制。
3、.h文件和.c文件请严格遵守要求中的约定。设定好的函数原型和.h文件请不要更改,并且理解为什么一字不能改?对于系统构架师(编写.h的工程师)和具体实现的工程师(编写.c文件工程师),以及测试工程师(编写main.c进行测试的工程师),和用户(调用库函数的人)之间是怎样合作,怎样沟通理解的?
4、请先自己尝试编写main函数进行测试(文件分离,再编写)