“FIRST, SOLVE THE PROBLEM,. THEN, WRITE THE CODE.”
STRING IN C:
IS STRING A DATA TYPE IN C?
- No
- We have char, int, float, and other data types but no string data type in C
- String is not a supported data type in C, but it is a very useful concept used to model real-world entities like name, city, etc.
- We express strings using an array of character terminated by a null character (”).
what are strings in c?
- String: an array of characters terminated by NULL character.
- String in C is created by creating an array of characters.
- We need an extra character ( or null character) to tell the compiler that the strings end here.
- When the compiler encounters a sequence of characters enclosed in the double quotation marks, it appends a null character at the end by default.
creating strings in c:
- We can create character array in the following ways:
- 1- char name [ ] = “harry” ;
- 2- char name [ ] = {‘h’, ‘a’, ‘r’, ‘r’, ‘y’,”};
read strings from the user:
You can use the scanf() function to read a string.
The scanf() function reads the sequence of characters until it encounters whitespace (space, newline, tab, etc.).
program to read a string using scanf():
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
output:
Enter name: Zoya Your name is Zoya.
taking string input from the user:
char str [52]; gets (str); // In string in place of scanf we can use gets to take input from the user.It is not compulsory that we can only use gets in string, we can also use scanf if we want. printf ("%s", str); puts(str); // In string in place of printf we can use puts to print the result or sentences.It is not compulsory that we can only use puts in string, we can also use printf if we want.
Let’s do some coding:
program-1
#include<stdio.h>
void printStar( char str[])
{
int i=0;
while(str[i]='')
{
printf("%c", str[i]);
i++ ;
}
}
int main ()
{
char str []="harry";
printStr(str);
return 0;
}
output:
harry
program 2- how to take input from the user
#include<stdio.h>
int main ()
{
char str [35];
printf("Enter your string n");
gets(str);
ptintf("String is %s n", str);
puts(str);
return 0;
}
c library: <string.h>
FUNCTION | USE |
strcat() | This function is used to concatenate or combine two given strings. |
strlen() | This function is used to show the length of a string without counting null character. |
strrev() | This function is used to show the reverse of a string. |
strcpy() | This function is used to copy one string into another. |
strcmp() | This function is used to compare two given strings. |
program-3 on strcat() :
#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;
}
output:
This is justhinter.com justhinter.com
program-4 on strlen()
#include <stdio.h>
#include <string.h>
int main( )
{
int len;
char array[20]="fresh2refresh.com" ;
len = strlen(array) ;
printf ( "\string length = %d \n" , len ) ;
return 0;
}
output:
string length = 17
program-5 on 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;
}
output:
Enter string: justhinter String is: justhinter Reverse String is: retnihtsuj
program-6 on strcpy:
#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;
}
output:
C programming
program-7 on strcmp()
#include <stdio.h>
#include <string.h>
int main( )
{
char str1[ ] = "fresh" ;
char str2[ ] = "refresh" ;
int i, j, k ;
i = strcmp ( str1, "fresh" ) ;
j = strcmp ( str1, str2 ) ;
k = strcmp ( str1, "f" ) ;
printf ( "\n%d %d %d", i, j, k ) ;
return 0;
}
output:
0-11
structure in c:
Arrays allow to define the type of variables that can hold several data items of the same kind. Similarly, structure is another user-defined data type available in C that allows to combine data items of different kinds. Structures are used to represent a record. In C programming, a struct (or structure) is a collection of variables (can be of different types) under a single name. Structures are used to represent a record.
how to define a structure?
To define a structure, you must use the struct statement. The struct statement defines a new data type, with more than one member. The format of the struct statement is as follows:
struct [structure tag] { member definition; member definition; ... member definition; } [one or more structure variables];
The structure tag is optional and each member definition is a normal variable definition, such as int i; or float f; or any other valid variable definition. At the end of the structure’s definition, before the final semicolon, you can specify one or more structure variables but it is optional.
create struct variable:
When a struct type is declared, no storage or memory is allocated. To allocate memory of a given structure type and work with it, we need to create variables.
Here’s how we create structure variables:
struct Person { char name[50]; int citNo; float salary; }; int main() { struct Person person1, person2, p[20]; return 0; }
Another way of creating a variable:
struct Person { char name[50]; int citNo; float salary; } person1, person2, p[20];
access structure members:
To access any member of a structure, we use the member access operator (.). The member access operator is coded as a period between the structure variable name and the structure member that we wish to access. You would use the keyword struct to define variables of structure type. Now, let’s take an example of “structure in C”.
program: on structure
Live Demo
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
/* print Book1 info */
printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
printf( "Book 1 subject : %s\n", Book1.subject);
printf( "Book 1 book_id : %d\n", Book1.book_id);
/* print Book2 info */
printf( "Book 2 title : %s\n", Book2.title);
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
printf( "Book 2 book_id : %d\n", Book2.book_id);
return 0;
}
output:
Book 1 title : C Programming Book 1 author : Nuha Ali Book 1 subject : C Programming Tutorial Book 1 book_id : 6495407 Book 2 title : Telecom Billing Book 2 author : Zara Ali Book 2 subject : Telecom Billing Tutorial Book 2 book_id : 6495700
why struct in c?
Suppose, you want to store information about a person: his/her name, citizenship number, and salary. You can create different variables name, citNo and salary to store this information.
What if you need to store information of more than one person? Now, you need to create different variables for each information per person: name1, citNo1, salary1, name2, citNo2, salary2,etc.
A better approach would be to have a collection of all related information under a single name Person
structure and use it for every person.
I wish now each topic is clear to you. The more you do practice the more concepts you get cleared. So always keep practicing until you got a command on it. In our next artcle we will discuss about "UNION IN C". I will see you next time... Also visit our previos articles listed below:
- C PROGRAMMING: RECURSION IN C
- C PROGRAMMING: FUNCTIONS
- C PROGRAMMING: TYPECASTING
- C PROGRAMMING: BREAK, CONTINUE, AND GOTO STATEMENTS
- C PROGRAMMING: LOOPS
AND MANY MORE, SO VISIT OUR SITE…https://justhinter.com/category/programming/