Strings
In C programming, a string is an array of characters terminated by a null character \0. Strings
in C are represented as arrays of characters, where each character corresponds to a single
element in the array. The string's end is marked by the null character, which has an ASCII
value of 0.
Character Array: Strings in C are essentially arrays of characters. For example:
char myString[10]; // Declares a string of 10 characters
Null Terminator: A string in C must end with a null character (\0). This character indicates the
end of the string.
String Initialization:
Strings can be initialized using double quotes:
char myString[] = "Hello";
Alternatively, you can initialize individual characters:
char myString[] = {'H', 'e', 'l', 'l', 'o', '\0'};
String Input: You can input strings using functions like scanf() or fgets():
char myString[50];
scanf("%s", myString); // Reads a string from user input without blanks
gets(myString); // reads a string from user with blanks
String Functions: C provides several string handling functions in the <string.h> library, such
as:
strlen(): Calculates the length of a string.
strcpy(): Copies one string to another.
strcmp(): Compares two strings.
strcat(): Concatenates two strings.
And more...
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[20];
// Copying str1 to str2
strcpy(str2, str1);
printf("str2: %s\n", str2);
// Concatenating " World" to str2
strcat(str2, " World");
printf("str2 after concatenation: %s\n", str2);
// Finding the length of str2
printf("Length of str2: %zu\n", strlen(str2));
// Comparing str1 and str2
if (strcmp(str1, str2) == 0) {
printf("str1 and str2 are equal.\n");
} else {
printf("str1 and str2 are not equal.\n");
}
return 0;
}
Remember, in C, strings are arrays of characters, and manipulating them requires careful
consideration of the null terminator and array boundaries to prevent buffer overflows and
other potential issues.
2. Develop a Program in C for the following operations on Strings.
a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP)
b. Perform Pattern Matching Operation
Find and Replace all occurrences of PAT in STR with REP if PAT exists in STR. Report
suitable messages in case PAT does not exist in STR. Support the program with
functions for each of the above operations. Don't use Built-in functions.
#include <stdio.h>
#include <string.h>
void findreplace(char str[],char pat[], char rep[], char newstr[]);
void main()
{
char str[50],pat[10],rep[10],newstr[50];
int i,j,k,m,pos;
printf("Enter the String: ");
gets(str);
printf("\nEnter the Pattern: ");
gets(pat);
printf("\nEnter the replacement String: ");
gets(rep);
findreplace(str,pat,rep,newstr);
printf("String after Replacement is : %s\n",newstr);
}
void findreplace(char str[],char pat[],char rep[], char newstr[])
{
int i,j,k,l=0;
for(i=0;str[i]!='\0';)
{
j=0;
while(pat[j] != '\0')
{
if(str[i+j]==pat[j])
j++;
else
break;
}
if(pat[j]=='\0')
{
printf("match found\n");
// replace string
for(k=0;rep[k]!='\0';k++,l++)
newstr[l]=rep[k];
//change index i of str after match
i=i+strlen(pat);
}
else
newstr[l++]= str[i++];
}
newstr[l]='\0';
}
OUTPUT
Enter the String: good morning!!!
Enter the Pattern: mor
Enter the replacement String: eve
match found
String after Replacement is : good evening!!!
OUTPUT
Enter the String: do good be good
Enter the Pattern: good
Enter the replacement String: GOOD
match found
match found
String after Replacement is : do GOOD be GOOD
OUTPUT
Enter the String: good afternoon good afternoon
Enter the Pattern: afternoon
Enter the replacement String: evening
match found
match found
String after Replacement is : good evening good evening
OR
#include <stdio.h>
#include <string.h>
int find(char str[],int pos,char pat[]);
int replace(char newstr[],int pos, char rep[]);
void main()
{
char str[50],pat[10],rep[10],newstr[50];
int i,j,k,m,pos;
printf("Enter the String: ");
gets(str);
printf("\nEnter the Pattern: ");
gets(pat);
printf("\nEnter the replacement String: ");
gets(rep);
k=0;
for(i=0;str[i]!='\0';)
{
pos = find(str,i,pat);
printf("position find = %d\n",pos);
for(j=i;j<pos;j++,k++)
{
newstr[k]=str[j];
printf("%c\n",newstr[k]);
}
i=pos+strlen(pat);
if(str[i]!='\0')
k = replace(newstr,k,rep);
printf("position k replace = %d i = %d\n",k,i);
}
newstr[k]='\0';
printf("new string is : %s",newstr);
}
int find(char str[],int pos, char pat[])
{
int i,j=0;
i=pos;
while(pat[j]!='\0' && str[i]!='\0')
{
if(str[i+j]==pat[j])
{
j++;
}
else
{
j=0;
i++;
}
}
if(pat[j]=='\0')
printf("found at position: %d",i);
else if(str[i]=='\0')
printf("not found\n");
return i;
}
int replace(char newstr[], int pos, char rep[])
{
int i,j;
for(j=0,i=pos;rep[j]!='\0';j++,i++)
{
newstr[i]=rep[j];
printf("%c \n",newstr[i]);
}
return i;
}