strlen

Usage of fgets function in C

泪湿孤枕 提交于 2019-11-29 11:28:51
One of my assignments in to write my own UNIX Shell. To receive input from the user, I am using fgets to capture the input as a string but I'm not really sure how it works. When I run: char command[50]; fgets(command, sizeof(command), stdin); printf("Your Command: %s", &command); int length = strlen(command); printf("Length of String: %d\n", length); Lets say my the input was "exit". strlen says that the string is 5 characters long, instead of four. I want to do this: if( (strcmp(command, "exit")) == 0 ){ doSomething(); } but command is never equaling the string that I want it to; its like it

why is -1>strlen(t) true in C? [duplicate]

流过昼夜 提交于 2019-11-29 11:05:55
This question already has an answer here: void main() { if(sizeof(int) > -1) printf(“true”); else printf(“false”); ; [duplicate] 3 answers Why sizeof(int) is not greater than -1? [duplicate] 2 answers Working on this little piece of code in VS2013, but for some reason it doesn't print.it seems that -1>strlen(str) Anyone got an idea what i'm doing wrong char *str="abcd"; if(-1<strlen(str)) printf("The size of the string is %d", strlen(str)); return 0; Anyone got an idea what i'm doing wrong strlen() returns a size_t , which is an unsigned integer type. -1 interpreted as an unsigned integer is a

Is strlen on a string with uninitialized values undefined behavior?

南楼画角 提交于 2019-11-29 10:48:46
strlen returns the number of characters that precede the terminating null character. An implementation of strlen might look like this: size_t strlen(const char * str) { const char *s; for (s = str; *s; ++s) {} return(s - str); } This particular implementation dereferences s , where s may contain indeterminate values. It's equivalent to this: int a; int* p = &a; *p; So for example if one were to do this (which causes strlen to give an incorrect output): char buffer[10]; buffer[9] = '\0'; strlen(buffer); Is it undefined behavior? M.M Calling the standard function strlen causes undefined

KMP算法

喜夏-厌秋 提交于 2019-11-29 10:29:42
长文章:https://www.cnblogs.com/ZuoAndFutureGirl/p/9028287.html 板题:求子串在主串中出现的次数 #include<bits/stdc++.h> using namespace std; const int M=1e6+6; char S[M],T[M]; int nextt[M]; int ans=0; void makeNext(){ int m=strlen(T); nextt[0]=0; for (int i=1,j=0;i<m;i++){ while(j>0&&T[i]!=T[j]) j=nextt[j-1]; if(T[i]==T[j]) j++; nextt[i]=j; } } void kmp(){ int n,m; n = strlen(S); m = strlen(T); makeNext(); for (int i=0,q=0;i<n;i++){ while(q>0&&T[q]!=S[i]) q=nextt[q-1]; if(T[q]==S[i]) q++; if(q==m) ans++; } } int main() { while(~scanf("%s%s",S,T)) { ans=0; kmp(); printf("%d\n",ans); } return 0; } View Code 来源:

Sql中的left函数、right函数

社会主义新天地 提交于 2019-11-29 08:37:11
DB2中left()函数和right()函数对应oracle中的substr()函数 DB2 LEFT、RIGHT函数 语法: LEFT(ARG,LENGTH)、RIGHT(ARG,LENGTH) LEFT、RIGHT函数返回ARG最左边、右边的LENGTH个字符串,ARG可以是CHAR或BINARY STRING。 eg:SELECT LEFT(NAME,2),RIGHT(NAME,2) FROM T1 ORACLE substr()函数 substr(字符串,截取开始位置,截取长度) //返回截取的字 substr('Hello World',0,1) //返回结果为 'H' *从字符串第一个字符开始截取长度为1的字符串 substr('Hello World',1,1) //返回结果为 'H' *0和1都是表示截取的开始位置为第一个字符 substr('Hello World',2,4) //返回结果为 'ello' substr('Hello World',-3,3)//返回结果为 'rld' *负数(-i)表示截取的开始位置为字符串右端向左数第i个字符 Oracle数据库中是没有left() 和right() 函数的,若想按照DB2中对应的函数去使用,自己新建两个function即可,方法如下 LEFT CREATE OR REPLACE FUNCTION "LEFT"

php 数据脱敏显示

微笑、不失礼 提交于 2019-11-29 07:04:34
/** * 数据脱敏 * @param $string 需要脱敏值 * @param int $start 开始 * @param int $length 结束 * @param string $re 脱敏替代符号 * @return bool|string * 例子: * dataDesensitization('13126989876', 3, 4); //131****9876 * dataDesensitization('张三四', 0, -1); //**四 */ function dataDesensitization($string, $start = 0, $length = 0, $re = '*') { if (empty($string)){ return false; } $strarr = array(); $mb_strlen = mb_strlen($string); while ($mb_strlen) {//循环把字符串变为数组 $strarr[] = mb_substr($string, 0, 1, 'utf8'); $string = mb_substr($string, 1, $mb_strlen, 'utf8'); $mb_strlen = mb_strlen($string); } $strlen = count($strarr);

sizeof 和 strlen 的区别

拜拜、爱过 提交于 2019-11-29 05:07:24
区别 1.sizeof 是运算符,strlen 是函数。 2.sizeof 可以用类型做参数,strlen 只能用 char* 做参数,且必须是以 \0 结尾的。 3.sizeof 操作符的结果类型是 size_t ,它在头文件中 typedef 为 unsigned int 类型。该类型保证能容纳实现所建立的最大对象的字节大小。 4.编译器在编译时就计算出了sizeof 的结果。而strlen 函数必须在运行时才能计算出来。并且 sizeof计算的是分配时数据类型占内存空间的大小,而 strlen 计算的是字符串实际的长度。 char str[20]="0123456789"; int a=strlen(str); // a=10, strlen 计算字符串的长度,以结束符 `'\0'` 为字符串结束。 int b=sizeof(str); // b=20, sizeof 计算的则是分配的数组 str[20] 所占的内存空间的大小, 不受里面存储的内容改变。 5.sizeof 后如果是类型必须加括弧,如果是变量名可以不加括弧。这是因为 sizeof 是个操作符不是个函数。 6.当适用一个结构类型或变量时,sizeof 返回实际的大小;当适用一静态地空间数组, sizeof 归还全部数组的尺寸;sizeof 操作符不能返回动态地被分派了的数组或外部的数组的尺寸。 7.数组做

Invalid arguments ' Candidates are: std::basic_ostream<char,std::char_traits<char>> & write(const char *, int) '

你。 提交于 2019-11-29 04:20:35
const char* p = "hello itcast"; cout.write(p,strlen(p)) << endl; cout.write(p,strlen(p) - 4) << endl; cout.write(p,strlen(p) - 4) << endl; 代码十分简单,通过对比vs,得知gcc调用的是ostream.tcc文件中的write函数write(const _CharT* __s, streamsize __n)。 vs中是定义在ostream文件中的write(const _Elem* _Str, streamsize _Count),gcc中有 write ( const char_type* __s, streamsize __n);源码地址 https://gcc.gnu.org/onlinedocs/gcc-4.7.4/libstdc++/api/a01220_source.html 来源: https://www.cnblogs.com/sageloris/p/11452268.html

How to find the length of argv[] in C

左心房为你撑大大i 提交于 2019-11-29 03:43:46
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]){ int fir; //badly named loop variable char *input[] = calloc( strlen(argv), sizeof(char)); //initializing an array for( fir = 1; fir< strlen(argv); fir++){ //removing the first element of argv strcat(input, argv[fir]); // appending to input } } The error I'm getting is for line 7. It says "passing argument 1 of 'strlen' from incompatible pointer type". I get the same error for the strcat function. It also says "given a char ** but expected a const char * " for both functions. I'm trying to populate a

strlen in the C preprocessor?

橙三吉。 提交于 2019-11-29 01:39:13
问题 Is it possible to implement strlen() in the C preprocessor? Given: #define MYSTRING "bob" Is there some preprocessor macro, X , which would let me say: #define MYSTRING_LEN X(MYSTRING) 回答1: It doesn't use the preprocessor, but sizeof is resolved at compile time. If your string is in an array, you can use that to determine its length at compile time: static const char string[] = "bob"; #define STRLEN(s) (sizeof(s)/sizeof(s[0])) Keep in mind the fact that STRLEN above will include the null