C Programming IT Programming Tutorials

Strings

Strings are the representation of the one-dimensional char type array, terminated by the null character. 

Each character occupies 1byte of memory and since the end of the array includes a null character, the size of the char array must be one more than the characters in an array.

 The Strings can be represented in one of two ways.

char newarr[7]={'S','t','r','i','n','g','\0'};

 or

char newarr[]="String";

Accessing and Modifying Strings:

Similar to an integer array each element of the string is accessed using the index of an array. The string array cannot be declared similar to an integer or float array but the entire string can be displayed using the format specifier %s.

Example 1:

Explanation: In the above example, the output string is displayed using the format specifier %s. Accessing the index position strarr[7] gives the character ‘P’ since the indexing starts from 0 and space is also counted as a character.

Assigning array index 0 with the value of p changes the starting letter of the word string to ‘p’.

Example 2:

Explanation:

The strstr() function returns the base address of the substring in the main string. The string compare function compares the first letter,

if the ASCII value of the first character matches with that of the first character in s2 then check the next character and if

s1 and s2 are equal, then returns 0.

If ASCII value of character in s1<s2 returns a negative value.

If  ASCII value of character in s1>s2 returns a positive integer value.

Predefined String Functions In C:

Some of the predefined functions such as strrev(), strupr(), strlwr()

Some of the predefined functions such as strrev(), strupr(), strlwr() can result in errors based on the type of compiler used.

Example 3:

Explanation:

The string operation such as concatenation, copy, comparison, and calculation of the length of the string is obtained using the predefined function.

The strcmp(s1,s2) gives 0 since the value of s2 is copied to s1 in the previous step, and hence both s1 and s2 have the same value “function”.

Manipulation of Strings Without Using Predefined Function: 

The pointers can be used to perform all string operations without using the predefined function.

Length Of A String:

Example 4:

Explanation:

In the above example, we declare a char pointer s2 and assign the base address os s1 to s2. The variable count is used to store the length of the string and initialized with the value 0. 

Since we assigned the base address to s2, the value of starting address can be obtained by using *s2. While loop is used to check each character until null is found and increases the value of the count and the address of s2. 

Once the condition becomes false that means the value inside the *s2 is null. Then it exits the loop and prints the count, which is the length of the string.

String Copy &Concatenation:

Example 5:

Explanation:

In the above example, the string copy and concatenation are done by passing array elements using a pointer. The changes made inside the function remain unchanged since the value is changed inside the original address. 

In StringCopy, each value inside s1 is assigned to the newstr[20] array until the value of s1 becomes null and the copied value is displayed.

In the StringConcat function, the value of s1 and s2 is passed inside the function. To bind the string s2 to the end of s1, the value of s1[i] is checked for null and i is incremented.

Once the value becomes null then the string s2 is checked for null and assigned to s1. The index position of i and j are incremented and assigned with null at the end.

Upper to Lower & Lower to Upper Case Conversion:

Example 6:

Explanation:

The upper to lower and lower to upper case conversions are done by adding and substracting  32 to ASCII values of each character of the string.

A to Z has ASCII value ranging from 65 to 90 and a to z range from 97 to 122, the difference between a to A is 32.

String Comparison:

Example 7:

Explanation: The string comparison function checks each character until one of the values of the string becomes null, to check that we find the string with minimum length.

Then we check each character of both the string, if it is different then the difference between the ASCII value of the character in a string is assigned to the result.

Important Case To know About scanf():

scanf(“%[^\n]”,s) 

The above-mentioned line is used to read strings with spaces until we press enter. Check the above link to learn about the problem that occurs while using this scanf function.

Recommended Articles

Leave a Reply

Your email address will not be published. Required fields are marked *