Strings

0 views - 1 minute readEdit this post

I rely on your support to keep this website running. If you find the content valuable, please consider making a small donation. whether it’s ₹25/$1 or ₹500/$5.

Donate

Secured by Stripe

Strings

  • String is collection of characters.
  • Strings are written in double quotation mark (" ").
  • Each string will get NULL character (\0) at the end of it.
  • Strings are basically One Dimensional Array of characters.

String Syntax

char str_name[str_size] = "str_content"

String Example

char [] = "Hello"; // you can leave str_size empty which will have no limit in str_content.

preview

  • Strings can contain all types of characters.
    • A to Z
    • a to z
    • 0 to 9
    • @, #, $, %, ^, etc.

Initialization of string

size defined string

char str[30] = "preetsuthar.me"

no size defined string

char str[] = "preetsuthar.me"

defining as array with size

char str[30] = {"p","r","e","e","t","\0"} // Null character at the end.

defining string as pointer

char *str = "hello"

Reading strings

Reading string using scanf()

  • We use scanf() in C language to read any input from user.
  • scanf() won't read anything after whitespace in input string.
"hello world" // it will only read till hello.
  char str[];
  scanf("%s", &s);

Reading string using getchar()

  • This will just read first character in string.

    "Hello world" // it will only read 'h'.
    
      char str[10];
      str = getchar()
    

Reading string using gets()

  • This will read entire string element with all the white spaces and all the new lines.
    Hello
    This will
    Read this string
    Exactly
    As it is.
char str[10];
gets(str);

Operations on string

detailed information will be covered in upcoming lectures

  • String Read.
  • String Write.
  • String Length Count.
  • String Case Conversion.
    • Uppercase To Lowercase and vice a versa.
  • String Concatenate.
  • String Copy.
  • String Comparing.
  • Substring.
    • Getting specified value from any index in string,
  • String Reverse.
  • String Append.
  • String insertion.
  • String Deletion.

Did you enjoyed the post?

Twitter (x)