當前位置:首頁 » 編程語言 » 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的值。