当前位置:首页 » 编程语言 » u8C语言strncmp
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

u8C语言strncmp

发布时间: 2022-09-21 09:55:31

c语言自己写strcpy函数怎么写

#include <stdio.h>
#define LEN 1024
/* */
static void *memcpy(void *d, const void * s, size_t n);
static void *memmove(void *d, const void *s, size_t n);
static char *strcpy(char *d, const char *s);
static char *strncpy(char *d, const char *s, size_t n);
/* link */
static char *strcat(char *s1, const char *s2);
static char *strncat(char *s1, const char *s2, size_t n);
/* len*/
static size_t strlen(const char *s);
/* cmp */
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
int memcmp(const void *s1, const void *s2, size_t n);
/* find */
void *memchr(const void *s, int key_char, size_t n);
char *strchr(const char *s, int key_char);
char *strstr(const char *s, const char *key_str);
size_t strcspn(const char *s1, const char *s2);
size_t strspn(const char *s1, const char *s2);
char *strpbrk(const char *s1, const char *s2);
/* other */
char *strtok(char *s1, const char *s2);
void *memset(void *s, int c, size_t n);
int main(void)
{
char buf[LEN] ;
char *src = "world";
char *ss = "oucch";

printf("strlen -> src[len] = %d\n", strlen(src));
printf("memcpy ->%s\n", (char *)memcpy(buf, src, strlen(src) + 1));
printf("buf -> %s\n", buf);
printf("memmove->%s\n", (char *)memmove(buf + 3,buf, strlen(buf) + 1));
printf("buf -> %s\n", buf);
printf("strcpy -> %s\n", strcpy(buf, src));
printf("buf -> %s\n", buf);
printf("strncpy -> %s\n", strncpy(buf, ss, 1));
printf("buf -> %s\n", buf);
printf("strcat -> %s\n", strcat(buf, src));
printf("buf -> %s\n", buf);
printf("strncat-> %s\n", strncat(buf, src, 1));
printf("buf -> %s\n", buf);
printf("strcmp : %d\n", strcmp("123", "1234"));
printf("strncmp : %d\n", strncmp("123", "1234", 3));
printf("strncmp : %d\n", strncmp("123", "1234", 5));
printf("memcmp: %d\n", memcmp("123", "1235", 4));
printf("strcspn: %d\n" ,strcspn("wangtianqing", "ang"));

return 0;}
static void * memcpy(void *d, const void *s, size_t n)
{
char *d1;
const char *s1;

for(d1 = d, s1 = s; 0 < n; --n)
*d1++ = *s1++;
return (d);
}
static void *memmove(void *d, const void *s, size_t n)
{
char *d1;
const char *s1;

d1 = d;
s1 = s;
if (s1 < d1 && s1 < d1 + n)
for(s1 += n, d1 +=n; 0 < n; --n)
*--d1 = *--s1;
else
for(; 0 < n; --n)
*d1++ = *s1++;
return (d);
}
static char *strcpy(char *d, const char *s)
{
char *d1;
const char *s1;

for(d1 = d, s1 = s; (*d1++ = *s1++) != '\0';)
;
return (d);
}
static char *strncpy(char *d, const char *s, size_t n)
{
char *d1;
const char *s1;

for(d1 = d, s1 = s; 0 < n && *s1 != '\0'; --n) {
*d1++ = *s1++;
}
for(; 0 < n; --n)
*d1++ = '\0';
return (d);
}
static size_t strlen(const char *s)
{
const char *s1;

for(s1 = s; *s1 != '\0'; ++s1)
;
return (s1 - s);
}
static char *strcat(char *s1, const char *s2)
{
char *st1;
const char *st2;

for(st1 = s1 ; *st1 != '\0'; ++st1)
;
for(st2 = s2; (*st1++ = *st2++) != '\0';)
;
return (s1);
}
static char *strncat(char *s1, const char *s2, size_t n)
{
char *st1;
const char *st2;

for(st1 = s1; *st1 != '\0'; ++st1)
;
for(st2 = s2; 0 < n && (*st1++ = *st2++) != '\0'; --n)
;
return (s1);
}
int strcmp(const char *s1, const char *s2)
{
for(; *s1 == *s2; ++s1, ++s2)
if(*s1 == '\0')
return (0);
return ((*(unsigned char *) s1 < *(unsigned char *) s2 ) ? -1: +1);
}
int strncmp(const char *s1, const char *s2, size_t n)
{
for(; 0 < n && *s1 == *s2; ++s1, ++s2)
if( *s1 == '\0')
return (0);
return (*(unsigned char *) s1 > *(unsigned char *) s2 ? -1 : +1);
}
int memcmp(const void *s1, const void *s2, size_t n)
{
const unsigned char *st1;
const unsigned char *st2;

for(st1 = s1, st2 = s2; 0 < n; ++st1, ++st2, --n)
if( *st1 != *st2)
return (*st1 < *st2 ? -1 : +1);
return 0;
}
void *memchr(const void *s, int key_char, size_t n)
{
const unsigned char uc = key_char;
const unsigned char *us;

for( us = s; 0 < n; --n, ++us)
if( uc == *us)
return ((void *)us);
return (NULL);
}
char *strchr(const char *s, int key)
{
const char *s1 = s;

for(; *s1 != '\0'; ++s1)
if(*s1 == key)
return ((char *)s1);
return (NULL);
}

size_t strcspn(const char *s1, const char *s2)
{
const char *sc1, *sc2;

for(sc1 = s1; *sc1 != '\0'; ++sc1)
for(sc2 = s2; *sc2 != '\0'; ++sc2)
if(*sc1 == *sc2)
return (sc1 - s1);
return (sc1 - s1);
}
size_t strspn(const char *s1, const char *s2)
{
const char *sc1;
const char *sc2;

for(sc1 = s1; *sc1 != '\0'; ++sc1) {
for(sc2 = s2; *sc2 != '\0'; ++sc2)
if(*sc1 == *sc2)
break;
if(*sc2 == '\0')
return (sc1 - s1);
}
return (sc1 - s1);
}
char *strpbrk(const char *s1, const char *s2)
{
const char *sc1, *sc2;

for( sc1 = s1; *sc1 != '\0'; ++sc1)
for( sc2 = s2; *sc2 != '\0'; ++sc2)
if (*sc1 == *sc2)
return ((char *)sc1);
return (NULL);
}
char *strstr(const char *s, const char *key_str)
{
if (*key_str == '\0')
return ((char *) s);
for(; (s = strchr(s, *key_str)) != NULL; ++s) {
const char *sc1, *sc2;

for (sc1 = s, sc2 = key_str; ;) {
if( *++sc2 == '\0')
return ((char *)s);
else if( *++sc1 != *sc2)
break;
}

}
return (NULL);
}
char *strtok(char *s1, const char *s2)
{
char *sbegin, *send;
static char *ssave = "";

sbegin = s1 ? s1 : ssave;
sbegin += strspn(sbegin, s2);
if( *sbegin == '\0') {
ssave = "";
return (NULL);
}
send = sbegin + strcspn(sbegin, s2);
if( *send != '\0')
*send++ = '\0';
ssave = send;
return sbegin;
}
void *memset(void *s, int c, size_t n)
{
const unsigned char uc = c;
unsigned char *su;

for( su = s; 0 < n; ++su, --n)
*su = uc;
return (s);
}

所有的函数原型都在这了 你想要哪个都行
谢谢采纳

⑵ 请在不调用C的字符串函数库的条件下,用c语言完成类似strncmp函数的功能。

int strcmp(char *src,char *dst,int count)
{
for(int i=0;i<count;i++)
{
if(*src!=*dst)
return (*src-*dst);
src++;
dst++;
}
return 0;
}
void strcpy(char *src,char *dst,int count)
{
for(int i=0;i<count;i++)
{
*dst=*src;
src++;
dst++;
}
}

⑶ 求解这个C语言编程

//(1)
#include <stdio.h>
bool lower(char ch) {
return ch >= 'a' && ch <= 'z';
}
bool upper(char ch) {
return ch >= 'A' && ch <= 'Z';
}
int main() {
char ch[2];
while (printf ("请输入字母:") && scanf ("%s", ch)) {
if (lower(ch[0]))
printf ("%s是小写字母\n", ch);
else if (upper(ch[0]))
printf ("%s是大写字母\n", ch);
else
printf ("%s不是字母\n", ch);
}
return 0;
}
//(2)
#include <stdio.h>
int main() {
double math, physical, chemistry;
while (true) {
printf("请输入数学成绩:");
scanf ("%lf", &math);
printf("请输入物理成绩:");
scanf ("%lf", &physical);
printf("请输入化学成绩:");
scanf ("%lf", &chemistry);
double sum = math + physical + chemistry;
double mathPhy = math + physical;
if (math >= 60 && physical >= 50 && chemistry >= 40 && (sum >= 200 || mathPhy >= 150)) {
printf ("学生的申请合格\n");
}
else {
printf ("学生的申请不合格\n");
}
}
return 0;
}
//(3)
#include <stdio.h>
int main() {
int age[20];
int cnt = 0;
for (int i = 0; i < 20; i ++) {
printf ("请输入第%d个人的年龄:", i + 1);
scanf ("%d", &age[i]);
if (age[i] < 50 || age[i] > 60)
continue;
cnt ++;
}
printf ("年龄在(50~60)的人数为%d\n", cnt);
return 0;
}
//(4)
#include <stdio.h>
int ya(int x) {//(a)嵌套if
if (x >= 0) {
if (x > 0)
return 1;
else
return 0;
}
else {
return -1;
}
}
int yb(int x) {//(b)else if
if (x > 0)
return 1;
else if (x == 0)
return 0;
else
return -1;
}
int yc(int x) {//条件判断语句
return x >= 0 ? (x > 0 ? 1 : 0) : -1;
}
int main() {
int x;
printf ("请输入x:");
scanf ("%d", &x);
printf ("y = %d\n", ya(x));
printf ("y = %d\n", yb(x));
printf ("y = %d\n", yc(x));
return 0;
}

⑷ c语言strncmp怎么对比字符串的前三位

strncmp(str1, str2, 3)用于比较str1和str2的前三个字符是否相同。

⑸ 关于strncmp函数的

strncmp
原型:extern int strcmp(char *s1,char * s2,int n);

用法:#include <string.h>

功能:比较字符串s1和s2的前n个字符。

说明:
当s1<s2时,返回值<0
当s1=s2时,返回值=0
当s1>s2时,返回值>0

举例:

// strncmp.c

#include <syslib.h>
#include <string.h>

main()
{
char *s1="Hello, Programmers!";
char *s2="Hello, programmers!";
int r;

clrscr();

r=strncmp(s1,s2,6);
if(!r)
printf("s1 and s2 are identical");
else
if(r<0)
printf("s1 less than s2");
else
printf("s1 greater than s2");

getchar();
clrscr();

r=strncmp(s1,s2,10);
if(!r)
printf("s1 and s2 are identical");
else
if(r<0)
printf("s1 less than s2");
else
printf("s1 greater than s2");

getchar();
return 0;
}
Tags: strncmp C

⑹ C语言中如何将文件中的某行的字符串读取出来

所谓的某行你没有给出确切的定义,通常读文件采用的方法是将文件内容的全部,或者部分保存在缓存中,如果你是想读取第几行,那么就可以使用字符指针,寻找缓存中的'\n',并进行计数,找到想要的行。
如果想读取的是某个关键字所在的行,同样是可以使用字符指针,使用strncmp方法寻找关键字所在的位置,或者直接使用strstr方法查找。
需求不明确的时候不容易确定最佳的查找方式。

⑺ C语言strncmp的问题

1.如果 cs 不判断是否到达尾部,如果 cs, ct 字符一样长,那么会比较 '\0'后面的数据,这样就不对了。
2.ct之所以不用比较,你已经说了,如果不等长,比较到结束符\0时,肯定返回不为0。

⑻ c语言:如何将字符串中指定的字符替换为另一个指定字符

需要准备的材料分别有:电脑、C语言编译器。

1、首先,打开C语言编译器,新建一个初始.cpp文件,例如:test.cpp。

⑼ 如何写自己的strncmp函数 c语言

这样才对:

intmystrncmp(constchar*dst,constchar*src,intn){
while(--n&&(*dst==*src))
dst++,src++;
return*dst-*src;
}

⑽ C语言如何比较一个字符串的前几位

直接用函数strncmp就行。用法:
#include<string.h>
int strncmp ( const char * str1, const char * str2, size_t n );
【参数】str1, str2 为需要比较的两个字符串,n为要比较的字符的数目。
字符串大小的比较是以ASCII 码表上的顺序来决定,此顺序亦为字符的值。strncmp()首先将s1 第一个字符值减去s2 第一个字符值,若差值为0 则再继续比较下个字符,直到字符结束标志'\0',若差值不为0,则将差值返回。例如字符串"Ac"和"ba"比较则会返回字符"A"(65)和'b'(98)的差值(-33)。注意:要比较的字符包括字符串结束标志'\0',而且一旦遇到'\0'就结束比较,无论n是多少,不再继续比较后边的字符。
【返回值】若str1与str2的前n个字符相同,则返回0;若s1大于s2,则返回大于0的值;若s1 小于s2,则返回小于0的值。