Strings in C

May 20, 2021 0 Comments

Strings in C

Strings in C, Anything enclosed in double-quotes treated as a string. There is no predefined data type to declare and use strings. So, we use character arrays to declare strings in C programs.

Every string terminates a special character called a NULL character.

Declaring and Initializing Strings:

the syntax for declaring a string is as shown below:

char str[10];

we can store an array of characters to implemented NULL characters implicitly.

char str[10] = “satheesh”;

we can store an array of characters is by character by a character we explicitly add the NULL character

char str1[10] = {‘s’, ‘a’, ‘t’, ‘h’, ‘e’, ‘e’, ‘s’, ‘h’, ‘\0’};

Reading String from the user:

we can use the scanf() function to read a string. scanf() Input format conversion.

int scanf(const char *restrict format, ...); 

char str[10];

scanf(“%s”, str);

Passing string to functions: we can pass strings to function arguments. for example

void func(char a[]) {

printf(“%s”, a);

}

char str = “kumar”;

func(str);

Strings and Pointers:

Similar to arrays, string names are decayed to pointers. Hence, you can use pointers to manipulate elements of the string.

#include<stdio.h>

int main() {
        char str[10] = "satheesh";
        printf("%s\n", str);
        
        char n[] = "nelavalli";
        char *p = n;
        printf("%ld %ld \n", sizeof(p), sizeof(*p));
        return 0;
}

String Manipulations:

Strings in C

Strlen: calculate the length of a string.

size_t strlen(const char *s);
The strlen() function returns the number of bytes in the string pointed to by s
#include<stdio.h>
#include<string.h>

int main() {
        char str[10] = "satheesh";
        printf("%s %ld \n", str, strlen(str));
        return 0;
}

Strcpy: Copy a string.

char *strcpy(char *restrict dest, const char *src);
The strcpy() function return a pointer to the destination string dest
#include<stdio.h>
#include<string.h>

int main() {
        char str[10] = "satheesh";
        char st[10];
        strcpy(st, str);
        printf("%s %s \n", str, st);
        return 0;
}

Strcat: concatenate two strings

char *strcat(char *restrict dest, const char *restrict src);
The strcat() function return a pointer to the resulting string dest
#include<stdio.h>
#include<string.h>

int main() {
        char str[10] = "satheesh";
        char st[10] = "kumar";
        strcat(str, st);
        printf("%s \n", str);
        return 0;
}

strchr: locate character in the string.

char *strchr(const char *s, int c);
The strchr() function returns a pointer to the first occurrence of the character c in the string s
#include<stdio.h>
#include<string.h>

int main() {
        char str[10] = "satheesh";
        char *st = strchr(str, 'e');
        printf("%s %s \n", str, st);
        return 0;
}

Strcmp: compare two strings

int strcmp(const char *s1, const char *s2);
strcmp() returns an integer indicating the result of the comparison, as follows:        
• 0, if the s1 and s2 are equal;        
• a negative value if s1 is less than s2;        
• a positive value if s1 is greater than s2. 
#include<stdio.h>
#include<string.h>
int main() {
        char str[10] = "satheesh";
        char st[10] = "satheesh";
        int i = strcmp(str, str);
        printf("%d \n", i);
        return 0;
}

Share This: