0% found this document useful (0 votes)
73 views

Character Array Strings

Uploaded by

Hariom Jadon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views

Character Array Strings

Uploaded by

Hariom Jadon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Strings

What is Character Array in C?


A String is a collection of Characters. Hence, to define a String,
we use a Character Array:
String in C programming is a sequence of characters terminated
with a null character ‘\0’. Strings are defined as an array of
characters. The difference between a character array and a
string is the string is terminated with a unique character ‘\0’.
#include<stdio.h>
int main()
{
char str[8];
printf("Enter a String: ");
scanf("%s", &str);
printf("%s", str);
}
#include<stdio.h>
void main ()
{
char s[20];
printf("Enter the string?");
scanf("%[^\n]s",s);
printf("You entered %s",s);
}
#include <stdio.h>
#define MAX 50
int main()
{
char str[MAX];

// MAX Size if 50 defined


fgets(str, MAX, stdin);
printf("String is: \n");

// Displaying Strings using Puts


puts(str);
}
• Note :The C library function char *gets(char *str) reads a line from
stdin and stores it into the string pointed to by str. It stops when
either the newline character is read or when the end-of-file is
reached, whichever comes first.
#include <stdio.h>
int main ()
{
char str[50];
printf("Enter a string : ");
gets(str);
printf("You entered: %s", str); //we can use puts(str)
return(0);
}
Warning will come that gets is dangerous function to be used
• Here we must also notice that we do not need
to use address of (&) operator in scanf to store
a string since string s is an array of characters
and the name of the array, i.e., s indicates the
base address of the string (character array)
therefore we need not use & with it.
4 Ways to Initialize a String in C

1. Assigning a string literal without size: String literals


can be assigned without size. Here, the name of the
string str acts as a pointer because it is an array.
char str[] = "GeeksforGeeks";
2. Assigning a string literal with a predefined
size: String literals can be assigned with a
predefined size. But we should always account for
one extra space which will be assigned to the null
character. If we want to store a string of size n then
we should always declare a string with a size equal
to or greater than n+1.
char str[50] = "GeeksforGeeks";
3. Assigning character by character with size: We can
also assign a string character by character. But we
should remember to set the end character as ‘\0’
which is a null character.
• char str[14] =
{ 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
4. Assigning character by character without size: We
can assign character by character without size with
the NULL character at the end. The size of the
string is determined by the compiler automatically.
• char str[] =
{ 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
#include <stdio.h>
#include <string.h>
int main()
{
// declare and initialize string
char str[] = "Geeks";
// print string
printf("%s\n", str);
int length = 0;
length = strlen(str);

// displaying the length of string


printf("Length of string str is %d", length);
return 0;
}
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "C programming";
char str2[20];

// copying str1 to str2


strcpy(str2, str1);

puts(str2); // C programming

return 0;
}
C strcmp()

• The strcmp() compares two strings character


by character. If the strings are equal, the
function returns 0.
• strcmp() Parameters
• The function takes two parameters:
• str1 - a string
• str2 - a string
Example: C strcmp() function
#include<stdio.h>
#include <string.h>
int main(){
char str1[20],str2[20];
printf("Enter 1st string: ");
fgets(str1,20,stdin);//reads string from console
printf("Enter 2nd string: ");
fgets(str2,20,stdin);
if(strcmp(str1,str2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}
Eg. 2
#include <stdio.h>
#include <string.h>
int main(){
int result;
char example1[50], example2[50];
strcpy(example1, "C programming at TechOnTheNet.com");
strcpy(example2, "C programming is fun");
result = strcmp(example1, example2);
if (result == 0)
printf("Strings are the same\n");
if (result < 0)
printf("Second string is less than the first\n");
return 0;
}
C strcat()
• In C programming, the strcat() function contcatenates
(joins) two strings.
• The function definition of strcat() is:
• char *strcat(char *destination, const char *source)
• It is defined in the string.h header file.
• strcat() arguments
• As you can see, the strcat() function takes two arguments:
• destination - destination string
source - source string
• The strcat() function concatenates the destination string
and the source string, and the result is stored in
the destination string.
Example: C strcat() function

#include <stdio.h>
#include <string.h>
int main() {
char str1[100] = "This is ", str2[] = "programiz.com";
// concatenates str1 and str2
// the resultant string is stored in str1.
strcat(str1, str2);
puts(str1);
puts(str2);
return 0;
}
strlwr() function in C
• The strlwr( ) function is a built-in function in C and is used to convert a given string into
lowercase.
• Syntax:
• char *strlwr(char *str); Parameter:
• str: This represents the given string which we want to convert into lowercase.
• Returns: It returns the modified string obtained after converting the characters of the
given string str to lowercase.
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "GEEKSFORGEEKS IS THE BEST";
// converting the given string into lowercase.
printf("%s\n",strlwr (str));
return 0;
}
strupr() function in c

• The strupr( ) function is used to converts a given string to uppercase.


• Syntax:
• char *strupr(char *str); Parameter:
• str: This represents the given string which we want to convert into uppercase.
• Returns: It returns the modified string obtained after converting the characters of the given
string str to uppercase.

// c program to demonstrate
// example of strupr() function.
#include<stdio.h>
#include<string.h>
int main()
{
char str[ ] = "geeksforgeeks is the best";
//converting the given string into uppercase.
printf("%s\n", strupr (str));
return 0;
}
C Reverse String: strrev()

#include<stdio.h>
#include <string.h>
int main(){
char str[20];
printf("Enter string: ");
gets(str);//reads string from console
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
return 0;
}
strstr() in C

• This function takes two strings s1 and s2 as an argument


and finds the first occurrence of the sub-string s2 in the
string s1. The process of matching does not include the
terminating null-characters(‘\0’), but function stops there.
• Syntax:
• char *strstr (const char *s1, const char *s2); Parameters:
• s1: This is the main string to be examined.
• s2: This is the sub-string to be searched in s1 string.
• Return Value: This function returns a pointer points to the
first character of the found s2 in s1 otherwise a null
pointer if s2 is not present in s1. If s2 points to an empty
string, s1 is returned.
• Example:
#include <stdio.h>
#include <string.h>
int main(){
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
for(i=0;i < length ;i++){
if(string1[i] != string1[length-i-1])
{
flag = 1;
} break;
}
if (flag) {
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}
return 0;
}
C Pointers

• A pointer is a variable that stores the memory


address of another variable as its value.
• A pointer variable points to a data
type (like int) of the same type, and is created
with the * operator.
• The address of the variable you are working
with is assigned to the pointer:
#include <string.h>
#include <stdio.h>

int main()
{
// Take any two strings
char s1[] = "GeeksforGeeks";
char s2[] = "for";
char* p;

// Find first occurrence of s2 in s1


p = strstr(s1, s2);
// Prints the result
if (p) {
printf("String found\n");
printf("First occurrence of string '%s' in '%s' is '%s'", s2, s1, p);
} else
printf("String not found\n");
return 0;
Passing Strings to Function
#include <stdio.h>

void printStr(char str[]) {


printf("String is : %s", str);

}
int main()
{
// declare and initialize string
char str[] = "GeeksforGeeks";
// print string by passing string
// to a different function
printStr(str);
return 0;
}
#include<stdio.h>
void main ()
{
int myAge = 43; // An int variable
int* ptr = &myAge; // A pointer variable, with the name ptr, that stores the
address of myAge

// Output the value of myAge (43)


printf("%d\n", myAge);

// Output the memory address of myAge (0x7ffe5367e044)


printf("%p\n", &myAge);

// Output the memory address of myAge with the pointer (0x7ffe5367e044)


printf("%p\n", ptr);
}
#include<stdio.h>
void main ()
{
int myAge = 43; // Variable declaration
int* ptr = &myAge; // Pointer declaration

// Reference: Output the memory address of myAge with the


pointer (0x7ffe5367e044)
printf("%p\n", ptr);

// Dereference: Output the value of myAge with the pointer (43)


printf("%d\n", *ptr);
}
Pointers with strings
• However, pointers can be used to point to the strings.
There are various advantages of using pointers to point
strings. Let us consider the following example to access the
string via the pointer.
#include<stdio.h>
void main ()
{
char s[11] = "javatpoint";
char *p = s; // pointer p is pointing to string s.
printf("%s",p); // the string javatpoint is printed if we print
p.
}
• As we know that string is an array of characters, the pointers can be
used in the same way they were used with arrays. In the above
example, p is declared as a pointer to the array of characters s.
• P affects similar to s since s is the base address of the string and
treated as a pointer internally.
• However, we can not change the content of s or copy the content of s
into another string directly. For this purpose, we need to use the
pointers to store the strings.
• In the following example, we have shown the use of pointers to copy
the content of a string into another.
#include<stdio.h>
void main ()
{
char *p = "hello javatpoint";
printf("String p: %s\n",p);
char *q;
printf("copying the content of p into q...\n");
q = p;
printf("String q: %s\n",q);
}
Once a string is defined, it cannot be reassigned to
another set of characters. However, using pointers,
we can assign the set of characters to the string.
Consider the following example.
#include<stdio.h>
void main ()
{
char *p = "hello javatpoint";
printf("Before assigning: %s\n",p);
p = "hello";
printf("After assigning: %s\n",p);
}
THANK-YOU

You might also like