banner



How To Change A Variable To String Code.org

String in C programming is a sequence of characters terminated with a goose egg character '\0'. Strings are defined as an assortment of characters. The difference between a character assortment and a string is the string is terminated with a unique character '\0'.

Example of C String:

C String Example
Declaration of Strings

Declaring a string is as elementary as declaring a one-dimensional array. Beneath is the basic syntax for declaring a string.

char str_name[size];

In the to a higher place syntax str_name is whatever proper name given to the string variable and size is used to define the length of the string, i.due east the number of characters strings will store.
Note: There is an extra terminating character which is the Goose egg character ('\0') used to bespeak the termination of a string that differs strings from normal character arrays . When a Sequence of characters enclosed in the double quotation marks is encountered by the compiler, a zip character '\0' is appended at the finish of the string past default.

Initializing a Cord

A cord tin be initialized in different ways. Nosotros will explain this with the help of an case. Below are the examples to declare a string with the name str and initialize it with "GeeksforGeeks".

four Means to Initialize a String in C

i. Assigning a cord literal without size: String literals can be assigned without size. Here, the name of the cord str acts every bit a pointer because information technology is an array.

char str[] = "GeeksforGeeks";        

2. Assigning a string literal with a predefined size: String literals tin can be assigned with a predefined size. Merely we should always account for ane actress space which will be assigned to the null grapheme. If we want to shop a string of size n then nosotros should always declare a cord with a size equal to or greater than n+1.

char str[50] = "GeeksforGeeks";

3. Assigning grapheme by graphic symbol with size: We can likewise assign a string graphic symbol by character. But we should call up to set the finish character as '\0' which is a zilch character.

char str[14] = { 'M','due east','east','k','s','f','o','r','G','eastward','e','k','due south','\0'};

4. Assigning character by grapheme without size: Nosotros can assign character by character without size with the Cipher character at the end. The size of the string is determined past the compiler automatically.

char str[] = { 'K','e','e','k','s','f','o','r','Chiliad','e','e','chiliad','due south','\0'};

Beneath is the memory representation of the string "Geeks".

Memory Representation of String in C

Allow us now look at a sample program to get a clear agreement of declaring, initializing a string in C, and as well how to impress a string with its size.

C

#include <stdio.h>

#include <cord.h>

int main()

{

char str[] = "Geeks" ;

printf ( "%southward\north" , str);

int length = 0;

length = strlen (str);

printf ( "Length of string str is %d" , length);

return 0;

}

Output

Geeks Length of string str is 5

We can see in the in a higher place plan that strings can be printed using normal printf statements just similar nosotros print whatsoever other variable. Unlike arrays, nosotros do non need to impress a string, character by grapheme.
Notation: The C linguistic communication does not provide an inbuilt data type for strings but it has an access specifier "%s" which can exist used to print and read strings directly.

How to Read a String From User?

C

#include<stdio.h>

int main()

{

char str[50];

scanf ( "%southward" ,str);

printf ( "%southward" ,str);

return 0;

}

          Input:          GeeksforGeeks          Output:          GeeksforGeeks

You tin see in the higher up programme that the cord can also be read using a single scanf statement. Also, y'all might be thinking that why we accept not used the '&' sign with the cord name 'str' in scanf statement! To empathize this you will have to call back your knowledge of scanf.
We know that the '&' sign is used to provide the address of the variable to the scanf() function to store the value read in memory. As str[] is a graphic symbol array so using str without braces '[' and ']' will give the base address of this string. That's why we have not used '&' in this case as nosotros are already providing the base of operations address of the string to scanf.

How to Read a Line of Text in C?

We can apply the fgets() function to read a line of string and gets() to read characters from the standard input  (stdin) and shop them as a C cord until a newline grapheme or the End-of-file (EOF) is reached.

For Example:

C

#include <stdio.h>

#define MAX 50

int master()

{

char str[MAX];

fgets (str, MAX, stdin);

printf ( "Cord is: \n" );

puts (str);

render 0;

}

          Input:          GeeksforGeeks          Output:          Cord is: GeeksforGeeks

Passing Strings to Role

Every bit strings are graphic symbol arrays, so nosotros tin laissez passer strings to function in the same way we pass an array to a function. Below is a sample program to practise this:

C

#include <stdio.h>

void printStr( char str[]) { printf ( "String is : %s" , str); }

int primary()

{

char str[] = "GeeksforGeeks" ;

printStr(str);

render 0;

}

Output:

String is : GeeksforGeeks

Note: Nosotros can't read a string value with spaces, we can use either gets() or fgets() in the C programming linguistic communication.

Strings and Pointers

In Arrays, the variable proper name points to the accost of the outset element. Similar to Arrays in C we tin create a graphic symbol pointer to a cord that points to the starting address of the string which is the first character of the string. The string can be accessed with the aid of pointers as shown in the below example.

C

#include <stdio.h>

int main()

{

char str[20] = "GeeksforGeeks" ;

char * ptr = str;

while (*ptr != '\0' ) {

printf ( "%c" , *ptr);

ptr++;

}

return 0;

}

Most Used Functions in C  Strings:

Function Name Description
strlen(string_name) Returns the length of string name.
strcpy(s1, s2) Copies the contents of string s2 to cord s1.
strcmp(str1, str2) Compares the outset cord with the second cord. If strings are the same it returns 0.
strcat(s1, s2) Concat s1 string with s2 string and the result is stored in the first string.
strlwr() Converts cord to lowercase.
strupr() Converts cord to uppercase.
strstr(s1, s2) Find the first occurrence of s2 in s1.

Must Read:

  • puts() vs printf() to impress a string
  • Swap strings in C
  • Storage for strings in C
  • gets() is risky to employ!

This article is contributed by Harsh Agarwal. If you similar GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks master folio and assist other Geeks.

Delight write comments if you observe annihilation wrong, or if you want to share more than information most the topic discussed to a higher place.


Source: https://www.geeksforgeeks.org/strings-in-c/

0 Response to "How To Change A Variable To String Code.org"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel