String processing 2-strcat/strcmp/strcpy/strchr/strrchr/strstr

Source: Internet
Author: User
Tags assert strcmp

Strcat

A simple strcat, I in the actual coding, or found the problem, first list

1. Mainly in Strcat_m, the beginning of Line8,while (tmp++!= ' ") and the following line9 is not the same.

Line8: After TMP points to ' "," and then shift back to a unit, so the last TMP point to ' the "after a unit

Line9: This is the right point to the ' yes '

2. In Line13, if you need to strcat_m many times, such as

c = strcat_m (a,b);

c = strcat_m (c,d);

The return--tmp will directly point to the last character, so it is convenient to continue the strcat and no longer need to traverse strdest to ' "and more efficiently.

#include <stdio.h>
#include <assert.h>

char* strcat_m (char* strdest, const char* STRSRC)
{
      assert (strdest!= null && strsrc!= null);
      char *tmp = strdest;
      while (*tmp++!= ' ");
      while (*tmp!= ' ")
          tmp++;
      while (*strsrc!= ' ")
          *tmp++ = *strsrc++;
      return--tmp;
      return strdest;
}
void main (void)
{
    char *buffer;
    Char str[100]  = "Hello";
    char* str1 = "World";
    Buffer = Strcat_m (STR,STR1);
    printf ("%s\n", buffer);
}

strcmp

#include <stdio.h>

int strcmp (const char* str1, const char* str2)
{
    int ret=0;
    while (!) ( ret= (*str1-*str2)) && *str1 && *str2)
    {
        str1++;
        str2++;
    }
    if (ret<0)
        return-1;
    else if (ret >0) return
        1;
    else return
        ret;
}
int main (void)
{
    char* p;
    char* str1 = "Hello";
    char* str2 = "Hellohe";
    printf ("%d\n", strcmp (STR1,STR2));
}

strcmp Another way of writing, considering the word length of case insensitive and CMP, which is actually the original form of Android.

int strncmp (char* s1, const char* s2, int n)
{
    assert (S1!= null && s2!= null);
    char tmp_s1,tmp_s2;
    int index=0;
    do{
        if ((tmp_s1 = *s1++) >= ' a ' && (tmp_s1 <= ' Z '))
            tmp_s1-= ' a '-' a ';
        if ((Tmp_s2 = *s2++) >= ' a ' && (tmp_s2 <= ' Z '))
            tmp_s2-= ' a '-' a ';
        index++;

    } while ((TMP_S1==TMP_S2) && tmp_s1 && index<n);
    return tmp_s1-tmp_s2;
}



strcpy

item1---->*dest = ' "; can return string or no return

ITEM2---->dest string must have a place to save, if there is no malloc, the program error

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h >
#include <assert.h>

void strcpy (char *dest,const char* src)
{
	assert (src!= NULL);
	while (*src!= ' ")
		*dest++ = *src++;
	*dest = ' i ';
}

int main (int argc, char* argv[])
{
	char *p = "Helloword";
	Char *q = (char *) malloc (a);
	strcpy (q,p);
	printf ("%s\n", q);

	return 0;
}

STRRCHR:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h >
#include <assert.h>

char *my_strrchr (char* p, char h)
{
	assert (p!=null);
	char *tmp=null;
	while (*p!= ' ") {
		if (*p = h)
			tmp = p;
		p++;
	}
	
	return tmp;
}

int main (int argc, char* argv[])
{
	char *p = "HelloWorld";
	Char *cout = MY_STRRCHR (P, ' l ');

	printf ("%s\n", cout);

	return 0;
}

STRCHR:

* * Plus a case-sensitivity judgment

Char *my_strchr (char* p, char h)
{
	assert (p!=null);

	if (h>= ' a ' && h<= ' Z ')
		h = n ' a ' + ' a ';

	while (*p!= ')
	{
		if (*p = = H | | (*p-' a ' + ' a ') = = h) return
			p;
		else
			p++;
	}

	if (*p = = ' = ') return
		NULL;
}

STRSTR:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h >
#include <assert.h>

char *my_strstr (char* p1, char* p2)
{
	assert (P1!=null && p2!= NULL);
	
	for (; *p1!= ';p 1++)
	{
		char *tmp_p1=p1;
		char *tmp_p2=p2;
		while (*tmp_p2!= ' && *tmp_p1!= ') {
			if (*tmp_p1 = = *tmp_p2) {
				tmp_p1++;
				tmp_p2++;
			}
			else break
				;
		}
		if (*tmp_p2 = = ' ") return
			p1
	}
	return NULL;
}

int main (int argc, char* argv[])
{
	char *p = "HelloWorld";
	Char *q = "Llo";
	Char *cout = MY_STRSTR (p,q);
	
	printf ("%s\n", cout);
	return 0;
}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.