Strings

A string is traditionally a sequence of characters, either as a literal constant or as some kind of variable. The latter may allow its elements to be mutated and the length changed, or it may be fixed( after creation).

A character array terminated by a '\0' (null character) null character denotes string termination

Example

char name[ ] = {'D', 'A', 'V', 'I', 'D','\0'}; char class[ ] = {'S', 'T', 'A','R' '\0'};

Initializing Strings

char name[ ] = {'D', 'A', 'V', 'I', 'D','\0'};
char class[ ] = {'S', 'T', 'A', 'R', '\0'};
char name[ ] = "DAVID";
char class[ ] = "STAR";

This is how memory works

D A V I D \0

2000 2001 2002 2003 2004 2005

Q1 Create a String firstName and LastName to store details of user of print all the characters using a loop in C.

#include <stdio.h>

void printString(char arr[]);
int main(){
  char firstName[]="Vasudev";
  char Name[]="Krishna";

  printString(firstName);
  printString(Name);
}

void printString(char arr[])
{
  for(int i=0;arr[i]!='\0';i++)
  {
    printf("%c",arr[i]);
  }
  printf("\n");
}

Important

scanf( ) cannot input multi-word strings with spaces.

Here, gets( ) & puts( ) come into picture

String Format Specifier

"%s"

printf("%s", name);

char name[ ] = "Shradha";

String Functions

gets(str) -input a string (even multiword)

puts(str) -output a string

fgets( str, n, file)

stops when n-1 chars input or new line is entered

Q2 Ask the user his first name and print it back to him. also try with full name in C.

#include <stdio.h>

void printString(char arr[]);
int main(){
  char str[100];
  printf("Enter yur name\n");
  gets(str);
  puts(str);

  return 0;
}

String Using Pointers

char *str = "Hello World";

Store string in memory & the assigned address is stored in the char pointer 'str'

char *str = "Hello World";

char str[ ] = "Hello World"; //cannot be reinitialized

Q3 Make a program that inputs user's name and prints ints length in C.

void printString(char arr[]);
int countlength(char arr[]);
int main(){
  char name[100];
  fgets(name,100,stdin);//count number of characters excluding '\0'

  printf("Length is:%d",countlength(name));
  return 0;
}
int countlength(char arr[]){
  int count=0;
  for(int i=0;arr[i]!=0;i++)
  {
    count++;
  }
  return count-1;
}

 void printString(char arr[])
 {
  for(int i=0;arr[i]!='\0';i++){
    printf("%c",arr[i]);
  }
  printf("\n");
 }

Standard Library Functions

<string.h>

1. strlen(str) count number of characters excluding '\0'

<string.h>

2. strcpy(newStr, oldStr) copies value of old string to new string

3 strcat(firstStr, secStr) concatenates first string with second string

4 strcmp(firstStr, secStr) compares 2 strings & returns a value

<string.h> (This inputs the library function for string)

0 -> string equal

positive -> first > second (ASCII)

negative -> first < second (ASCII)

Q4 Find the salted form of a password entered by the user if the salt is"123" & added at the end in C. (salting is how a password is set)

#include <stdio.h>
#include <string.h>
void salting(char password[]);
int main() {
    char password[100];
    scanf("%s",password);
    salting(password);

} 
void salting(char password[])
{
    char salt[]="123";
    char newpass[200];

    strcpy(newpass,password);//newpass=password
    strcat(newpass,salt);
    int a =strlen(newpass);
    printf("Length of password=%d\n",a);
    puts(newpass);
}

Q5 Write a function names slice which takes a string and returns a sliced string from n to m in C.

void slice(char str[],int n,int m);
int main() {
   char str[]="helloworld";
    slice(str,3,6);
} 
void slice(char str[],int n,int m)
{
    char sli[100];
    int j=0;
    for(int i=n;i<=m;i++, j++)
    {
        sli[j]=str[i];
    }
    sli[j]='\0';
    puts(sli);
}

Q4 Write a function to count the occurrence of vowels in a string in C.

int countv(char str[]);
int main() {
   char str[100]="Himynameisgagan";
   printf("vowels are:%d",countv(str));

} 
int countv(char str[]){
    int count=0;
    int n;
    for(int i=0;str[i]!='\0';i++)
    {
    if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u')
    {count=count+1;
     n=count;
    }
    }
    return n;
}

Q5 Cheak if a given character is present in a string or not in C.

void cheakcharacter(char str[],char ch);
int main() {
   char str[]="Himynameisgagan";
   char ch='H';
   cheakcharacter(str,ch);

} 
void cheakcharacter(char str[],char ch){
    for(int i=0;str[i]!='\0';i++)
    {
        if(str[i]==ch){
            printf("Character is present");
            return;
        }
    }
    printf("Character is not present");
}

Hope you enjoyed learning about Strings, this helps us storing text/characters, hope this blog was useful. And don't forget to follow and share this blog with a friend who wants to learn about programming.

Thank you...