Unit 4 - I Strings v1.5
Unit 4 - I Strings v1.5
Contents
1. Strings in C 3
1.1 Common operations on strings 3
1.2 Declaring and initializing strings 3
Initializing: 3
1. Strings in C
❑ In C, String is a sequence of characters treated as a single data item.
❑ E.g. “Hello world”, “Well done”
❑ Note: double quotes not part of string data.
❑ In C, Strings are represented using character arrays.
❑ E.g: char name[20];
Initializing:
❑ E.g.1: char city[9] = “New York”;
❑ E.g. 2: char city[9] = {‘N’,’e’,’w’,'',’Y’,’o’,’r’,’k’,’\0’};
❑ Null character ‘\0’ inserted by compiler in e.g.1.
❑ Specify array size considering ‘\0’ also.
❑ E.g.: char city[9] = “New York”;
char str1[9] ;
str1 = “Good”; // Error: Not allowed to assignment like this in C
❑ Null character(\0) marks the end-of-string.
PRG416:String: compile time initialization
❑ General format
scanf(“%ws”,string_variable);
char str[50];
printf("Enter the line of text:");
scanf("%[^\n]",str);
printf("Text is: %s",str);
❑ Note: The above method may not work well with all compilers.
❑ Reading string from user(prg419)
❑ gets() function to read a line.
char str[50];
printf("Enter the line of text:");
gets(str); // 1
printf("Text is:%s\n",str); // 2
❑ 1 & 2 may be combined as:
printf("Text is:%s\n", gets(str));
❑ printf(“String is: %9.5s”,str); //9🡺 reserved screen width, 5🡺 no. of chars to display
return 0;
}
Output:
Enter first string: hello
Length of the entered string = 5
Output:
Enter string s1: hello
String s2: hello
4.3 Concatenate Two Strings Without Using built-in function strcat()
Approach: Find the index of ‘\0’ character in the destination array by running a loop.
for(i =0; s1[i]!='\0';++i);
Now variable i has the index value of ‘\0’ in string s1. In a new loop take each character from source array and put in
to destination array s1 at index starting from i,i+1….etc. until all characters of source string s2 are copied. At last put a
‘\0’ at the end of all characters in destination array s1.
#include<stdio.h>
int main()
{
char s1[100], s2[100], i, j;
printf("Enter first string: ");
scanf("%s", s1);
s1[i]='\0';
printf("After concatenation: %s", s1);
return0;
}
Output:
Enter first string: hello
Enter second string: world!
After concatenation: helloworld!
int main(){
char str[20];
printf("strlen is %d\n",len);
return 0;
}
Output:
Enter the word
welcome
strlen is 7
Reversed string is: emoclew
#include <stdio.h>
int main()
{
char s1[100], s2[100], i, j;
if(s1[i] == '\0' && s2[i] == '\0') //if both strings are over
{
//Both strings are equal(same)
printf("0") ;
}
return 0;
}
O/p:
1.
Enter first string: abcd
Enter second string: abcd
0
2.
Enter first string: abCd
Enter second string: abcd
-1
3.
Enter first string: abcd
Enter second string: abCd
1
4.
Enter first string: abcd
Enter second string: abcdef
-1
5.
Enter first string: abcdef
Enter second string: abcd
1
for(i=0;i<n;i++)
{
printf("Enter next name:");
scanf("%s",names[i]);
}
return 0;
}
Sample I/O
How many names: 5
Enter next name:Rahul
Enter next name:Rohan
Enter next name:Amit
Enter next name:Shashank
Enter next name:Balu
1. strcat()
Concatenates two strings.
Usage: strcat(str1,str2);
o/p:
HelloWorld
o/p:
Enter string 1:hello
Enter string 2:world
Concatenated string:helloworld
❑ Some implementations return the difference of the ASCII values of first mismatching characters.
i.e strcmp("their","there"); // return -9 which is the difference of ascii values of i and r.
int r = strcmp(str1,str2); // r = -1
r = strcmp("abcd","abcd"); // r=0
r = strcmp("abcd","abCd"); // r=1
r = strcmp("abcd","abfd"); // r=-1
r = strcmp("abfd","abcd"); // r=1
r = strcmp("ABCD","abcd"); // r=-1
}
o/p
Copied string: Hello World
return 0;
}
o/p:
Enter a string: hello