The gets (), puts () function. A string function. Examples of string sorting.

Source: Internet
Author: User
Tags first string strcmp

1, instance program: STRING.C Program:

12345678910111213141516171819202122232425262728 #include<stdio.h>#define MSG "YOU MUST have many talents .tell me some."#define LIM 5#define LINELEN 81intmain(){char name[LINELEN];chartalents[LINELEN];inti;constcharm1[40]="limit yourself to one line‘s worth.";constcharm2[]="IF you can‘t think of your anything,fake it.";constchar*m3="\nENough about me,what‘s your name?";constchar*mytal[LIM]={"adding numbers swiftly","mulityplying accurately","stashing data","flowing instructions to the letter","understanding C language"};//初始化一个字符串指针数组printf("hi , i‘m clyde the computer.""i have many talents.\n");printf("let me tell you some talents.\n");puts("what were they?");for(i=0;i<LIM;i++)puts(mytal[i]);puts(m3);gets(name);printf("well, %s,%s\n",name,MSG);printf("%s \n %s\n",m1,m2);gets(talents);puts("let me see if i have got that  list:");puts(talents);printf("thanks for the information .%s.\n",name);return0;}

Operation Result:

As you can see, there are ways to define strings: using string constants, char arrays, char pointers, string arrays,

2. Consider a string as a pointer:

Instance Program:

123456 #include<stdio.h>intmain(){printf("%s,%p,%c\n","we","are",*"spare farers");return0;}

The%s format output string "We", the%p format produces a hexadecimal address, so if "is" is an address, then%p should output the address of the first character in the string. At last

1 *"spare farers"应该产生所指向的地址中的值,即字符串*"spare farers"的第一个字符。

3, strlen () to get the length of the string, shorten the string function

Sample program:

123456789101112131415161718 #include<stdio.h>#include<string.h>voidfit(char*,unsigned int);intmain(void){charmesg[]="Hold on to your heads,hackers.";puts(mesg);fit(mesg,7);puts(mesg);puts("let‘s look at some more of the string.");puts(mesg+8);return0;}voidfit (char*string,unsigned intsize){if(strlen(string)>size)*(string+size)=‘\0‘;}

Operation Result:

The Fit () function places the 8th element of the array in a

1 ‘\0‘,代替原有的空格字符,put函数输出时停在了第一个空格符处。忽略数组的其他元素,然而数组的其他元素仍然存在,mesg+8表示mesg[8]即‘t‘字符的地址,因此puts函数继续输出,直到遇到原字符串中的空字符。

4, strcat () represents the (string concatenation) function. The function accepts two string arguments, which add a copy of the second string to the end of the first string, so that the first string is called a new combined string, and the second string does not change. The function is a char* type (a pointer to char), which returns the value of its first argument, the address of the first character of the string after which the second string was added. The approach and ingredients of the plate surface

Instance Program:

1234567891011121314 #include<stdio.h>#include<string.h>#define size 80intmain()charflower[size];charaddon[]="s smell like old shoes,";puts("what‘s your favorite flowes?");gets(flower);strcat(flower,addon);puts(flower);puts(addon);return0;}

Operation Result:

5, the Strncat () function, the Strcat function does not check whether the first array can hold the next second string. If you do not allocate enough space to the first array, the extra characters overflow into adjacent storage units. Use the Strncat () function at this time. This function requires an additional parameter to indicate the maximum number of characters allowed to be added, such as Strncat (bugs,addon,13), which adds the contents of the addon to the bugs until it is added to 13 characters or until a null character is encountered.

6, strcmp () function. The user's response is compared to an existing string. Represents (String Comarison) strcmp (A, A, b), and returns a value of 0 if the parameters of the two string are the same. The comparison is a string, not an array. Used to compare strings rather than characters.

Wait a minute.

7, an example of string ordering

Let's take a look at an example of sorting a string alphabetically by alphabet. Mainly used in strcmp ()

Sample program:

12345678910111213141516171819202122232425262728293031323334353637 #include<stdio.h>#include<string.h>#define size 81#define lim 20#define halt " "//用空字符终止输入voidstart(char*string[],intnum);//字符串排序函数intmain(){charinput[lim][size];char*ptstr[lim];intct=0;intk;printf("input up to %d lines,and i will sort them.\n",lim);printf("to stop.press the enter key at a lines start\n");while (ct<lim&& gets(input[ct])!=NULL&&input[ct][0]!=‘\0‘){ptstr[ct]=input[ct];ct++;}start(ptstr,ct);puts("\n here the soreted list:\n");for(k=0;k<ct;k++)puts(ptstr[k]);return0;}voidstart(char*string[],intnum){char*temp;inttop,seek;for (top=0;top<num-1;top++)for(seek=top+1;seek<num;seek++)if(strcmp(string[top],string[seek])>0){temp=string[top];string[top]=string[seek];string[seek]=temp;}}

Operation Result:

The gets (), puts () function. A string function. Examples of string sorting.

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.