

Strings in C
Presentation
•
Computers
•
University
•
Practice Problem
•
Hard
Santhi Priya B
FREE Resource
7 Slides • 0 Questions
1
Strings in C
2
Strings in C
3
String functions in C with examples
What are strings?
In simple language STRING'S are nothing but the character array. The declaration of string
(character array) is much similar to normal array declaration. Each string is terminated by
'\0' as indication of string termination. So obviously you will require an extra byte of
memory for storing '\0'.'\0' is single character whose ASCII (American Standard Code for
Information Interchange) value is 0.
How to initialize strings (char array)?
char site[]={'g','o',o','g','l','e'.};
char Site[]="Programming spark";
How to take Input as string and print Output that string?
way 1:
void main()
{
char name[30];
printf("Enter your name");
scanf("%s", name);
//format specifier
printf("%s", name);
}
way 2:
void main()
{
char name[30];
printf("Enter your name");
gets(name);
//in-built function
puts(name);
}
Difference between both ways is that 1st way use's format specifier and second way uses
in-built function.
In 1st way if you Enter something like john karter it will take only john as string and
anything after (space) will be discarded. In second way if you enter any space it will be
accepted. Output of second way would be john karter.
Similarity between both way is that you don't have to add '\0' character.
scanf(%s ) converts 32(space) or '\n' character into '\0' and gets() converts newline
character '\n' into '\0'.
4
List of (most frequently used) string library functions in c.
strlen
strcpy
strcat
strrev
strcmp
strcmpi
strupr
strlwr
strncmpi
strncat
strncmp
strncmpi
strncmpi.
String functions examples
1) int strlen(char array):This function accepts string as parameter and return integer i.e
the length of String passed to it.
Example
#include <stdio.h>
#include <string.h>
void main(void)
{
char string[]="spark";
int len;
len=strlen(string);
printf("length of %s is %d\t", string, len);
}
Output::length of spark is 5.
Did you notice that strlen() does not include '\n' in string length or else length would be 6.
2) strcpy (Destination string,source string):This function accepts 2 strings as
parameter,1st one is destination string and 2nd is source string. This function copies source
string to destination string.
Example
#include <stdio.h>
#include <string.h>
void main(void)
{
char src[]="spark",dest[15];
strcpy(dest,src);
printf("%s is copied to dest string\t",dest);
}
Output: spark is copied to dest string.
3) strcat (Destination string,source string): This function accepts two strings source
string is appended to the destination string.
Example
#include <stdio.h>
#include <string.h>
5
void main(void)
{
char src[]="spark",dest[]="programming";
strcat(dest,src);
printf("concatenated string is %s",dest);
}
Output: concatenated string is programmingspark
4) strrev (string):This function accepts single string as parameter and reverse that string.
Example
#include <stdio.h>
#include <string.h>
void main(void)
{
char string[]="spark";
strrev(string);
printf("reverse string is %s",string);
}
Output: reverse string is kraps.
5)int strcmp (string 1, string2):This function compares two strings passed as
parameters and returns either +ve number,0,-ve number.
+ve value indicates string1 > string2.
0 indicates string1 and string2 are equal
-ve value indicates string1 < string2.
Example
#include <stdio.h>
#include <string.h>
void main(void)
{
char string1[]="spark",string2[]="programming";
int cmp;
cmp=strcmp(string1,string2);
if(cmp>0)
printf("%s > %s",string1,string2);
else
{
if(cmp<0)
printf("%s < %s",string1,string2);
else
printf("%s = %s",string1,string2);
}
}
6
Output: spark > programming.
.this is because alphabetically p comes first then s.so the string compare function returns
difference between ascii of s and p which would be +ve.
6) strcmpi (string 1, string2):This function is similar to strcmp().The onlyy difference is
that it ignores the case.example SparK and spark both are same.
Example
#include <stdio.h>
#include <string.h>
void main(void)
{
char string1[]="spark",string2[]="SPArk";
int cmp;
cmp=strcmpi(string1,string2);
if(cmp>0)
printf("%s > %s",string1,string2);
else
{
if(cmp<0)
printf("%s < %s",string1,string2);
else
printf("%s = %s",string1,string2);
}
}
Output: spark = SPArk.
7) strlwr (string)::This function accepts single string that can be in any case(lower or
upper).Itconverts the string in lower case.
Example
#include <stdio.h>
#include <string.h>
void main(void)
{
char string1[]="SPArk";
strlwr(string1);
printf("%s is in lower case",string1);
}
Output: spark is in lower case.
7
8) strupr (string)::This function accepts single string that can be in any case(lower or
upper).Itconverts the string in upper case.
Example
#include <stdio.h>
#include <string.h>
void main(void)
{
char string1[]="SPArk";
strupr(string1);
printf("%s is in upper case",string1);
}
Output: SPARK is in upper case.
Strings in C
Show answer
Auto Play
Slide 1 / 7
SLIDE
Similar Resources on Wayground
10 questions
Tempos Verbais
Presentation
•
12th Grade
10 questions
Reading advanced unit 9
Presentation
•
12th Grade - Professi...
10 questions
Technology Instruction
Presentation
•
Professional Development
10 questions
Reading - The Phenomenon Of Lightening
Presentation
•
Professional Development
7 questions
Python Basics
Presentation
•
12th Grade
6 questions
l1
Presentation
•
University
10 questions
PROGRAMACIÓN EN JAVA
Presentation
•
12th Grade
11 questions
Declaración de variables Bach
Presentation
•
12th Grade
Popular Resources on Wayground
6 questions
PRIDE Always and Everywhere
Presentation
•
12th Grade
20 questions
Lab Safety Quiz
Quiz
•
6th Grade
26 questions
KOR Review Kahoot Questions
Quiz
•
8th Grade
21 questions
Continents and Oceans
Quiz
•
6th Grade
12 questions
Unit Zero lesson 2 cafeteria
Presentation
•
9th - 12th Grade
20 questions
Parts of Speech
Quiz
•
5th Grade
10 questions
Riddles Riddles fun Riddles
Quiz
•
6th - 12th Grade
16 questions
Subject & Predicate
Quiz
•
5th Grade
Discover more resources for Computers
10 questions
Soup, Salad, or Sandwich?
Quiz
•
1st Grade - Professio...
20 questions
Disney Trivia
Quiz
•
University
8 questions
Extracted Quiz
Quiz
•
University
23 questions
Subject Verb Agreement
Quiz
•
9th Grade - University
67 questions
Unit 3 Random Questions
Quiz
•
11th Grade - University
64 questions
USHC 4 Key Terms 22-23
Quiz
•
11th Grade - University
12 questions
This or That...
Quiz
•
1st Grade - University
25 questions
Comma CFA
Quiz
•
6th Grade - University