C and C character strings

Source: Internet
Author: User
Tags array definition

C and C character strings

The C language usually contains characters and strings, while a string or string is actually an array.

Character array Definition

char arr[]={'h','e','l','l','o','\0'};

The defined string:

char arr1[]="HELLO";

The character input and output can use scanf and printf as the one-dimensional array, and the character can also use its own specific input and output functions gerchar and putchar, while using getchar and putchar to input a string of characters

    char arr[1000];    int i=0,j=0;    char ch;    while ((ch=getchar())!='\n') {        arr[i]=ch;        i++;    }    arr[i]='\0';    while (arr[j]!='\0') {        putchar(arr[j]);        j++;    }    printf("\n");

Output result:

Strings also have their own specific input and output functions.

// Input and Output char ch [100]; gets (ch); puts (ch );

 

Function section of the string: the header file needs to be imported.

#include <string.h>

 

Char str1 [30] = "wfds"; char str2 [] = "zfds"; strcpy (str1, str2); // copy str2 to str1, str1 is longer than str2 (str1); puts (str2); strcat (str1, str2); // link str2 to str1, total length space greater than two puts (str1); puts (str2); printf ("len = % lu \ n", strlen (str1 )); // calculate the length of the string printf ("len = % lu \ n", strlen (str2); // do not include '\ 0' printf ("% d \ n ", strcmp (str1, str2 ));

Result:

 

Character functions: the header file needs to be imported.

#include <ctype.h>

 

Char ch = 'A', substring = 'a'; printf ("% d \ n", isalpha (ch )); // whether it is the letter printf ("% d \ n", isupper (ch); // whether it is uppercase printf ("% d \ n", islower (ch )); // whether it is lowercase printf ("% d \ n", isdigit (ch); // whether it is a digital printf ("% c \ n", toupper (ch )); // convert to uppercase printf ("% C \ n", tolower (lower); // convert to lowercase

The string is written in uppercase and lowercase.

    char ch[100],ch1;    gets(ch);    int i=0;    while (ch[i]!='\0') {        ch1=ch[i];        if (isupper(ch1)==1) {           ch1= tolower(ch1);        }else{            ch1=toupper(ch1);        }        putchar(ch1);        i++;    }    printf("\n");

Convert string to integer or floating point type

Header files need to be imported

#include <stdlib.h>

 

// Convert the string to char * chs = "11.52"; printf ("chs = % s \ n", chs); double d = atof (chs ); int a = atoi (chs); printf ("% f \ n", d); printf ("% d \ n", );

Numeric to string

Int num = 1000; char chs [100]; // store num to sprintf (chs, "% d", num) in % d format ); printf ("chs = % s \ n", chs); // stores the sprintf (chs, "% 10 s", "asdf") strings in the specified format "); printf ("chs = % s", chs );

 

Related Article

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.