recent posts

String | C Programming Strings (With Examples)


Hi, I'm Golam Rabbani. Today I will discuss about string
in c program.
Greetings! How are you all? Hope everyone is well.
At first we have to know that what is string?
Answer: Strings are defined as an array of characters.
String terminated by a null character '\0'.
you must use in string.h header file.
For e.g. “string”;


s
t
r
i
n
g
\0
How to declare a string?
Char s[4];
s[0]
s[1]
s[2]
s[3]


How many way to initialize strings?
Type 1: char s[15] = “Hello world”;
Type 2: char s[] = “Hello world”;
Type 3: char s[15] = {‘H’,’e’,‘l’,’l’,‘o’,’\0’};
Type 4: char s[] = {‘H’,’e’,‘l’,’l’,‘o’,’\0’};
Let’s go……..
Example 1: Read a string.
#include<stdio.h>
#include<string.h>
int main(){
    char name[55] = "My name is Golam Rabbani";
    printf("%s", name);
    return 0;
}
// Just Basic
Output: My name is Golam Rabbani
Example 2: scanf() to read a string.
#include<stdio.h>
#include<string.h>
int main(){
    char name[55];
    printf("Enter string: ");
    scanf("%s", &name);
    printf("Output: %s", name);
    return 0;
}
// Use scanf
Enter string: Hi user
Output: Hi user
Example 3: gets() & puts() to read a string.
#include<stdio.h>
#include<string.h>
int main(){
    char name[55];
    printf("Enter character: ");
    gets(name);//for input character.
    printf("Output: ");
    puts(name);//for output character.
    return 0;
}
// Use gets & puts
Enter character: Hello world!
Output: Hello world!
Type of String function:

1. strlen() - calculates length of a string
2. strcpy() - copies a string to another
3. strcmp() - compares two strings
4. strcat() - concatenates two strings
5. strchr() - searches the occurrence of a specified
character in the given string.


#strlen(): length of string.
e.g. H e l l o
    1 2 3 4 5
Length = 5


Question 1: Write a program to find the string length.
#include<stdio.h>
#include<string.h>
int main(){
    char name[55] = "Golam Rabbani";
    int count;
    count = strlen(name);
    printf("%d", count);
    return 0;
}
// String Length
Length: 13
Question 2: Write a program to find the string length
without using strlen() function.
#include<stdio.h>
#include<string.h>
int main(){
    char name[55];
    int i,count=0;
    printf("Enter string: ");
    gets(name);
    for(i=0; name[i]!='\0'; i++){
        count++;
    }
    printf("Length: %d",count);
    return 0;
}
// String length without
// build in function.
Enter string: hello
Length: 5
#strcpy(): Copies a string to another.
e.g. a=50; and b=20;
If strcpy(a,b); a←b
Result a=20
Program:
#include<stdio.h>
#include<string.h>
int main(){
    char fname[55] = "Golam";
    char lname[55] = "Rabbani";
    strcpy(fname, lname);
    printf("%s", fname);
    return 0;
}
//just copy and change
Output: Rabbani
#strcmp(): Compares two strings.
e.g. string a=bd; and string b=bd;
If a==b
Right Answer
Else
Wrong Answer
Program:
#include<stdio.h>
#include<string.h>
int main(){
    char a[10] = "Hi";
    char b[10] = "Hi";
    if(strcmp(a,b)==0){
        //if a==b, result 0
        //so 0==0 right.
        printf("Right Answer!");
    }
    else{
        printf("Wrong Answer!");
    }
    return 0;
}
Output: Right Answer!
#strcat(): Concatenates two strings.
e.g. a=”Hi”; b=”world”;
Concatenates means a+b;
If a+b; result is Hi world.
Program:
#include<stdio.h>
#include<string.h>
int main(){
    char fname[55] = "Hi,";
    char lname[55] = "Golam Rabbani";
    strcat(fname, lname);
    printf("%s\n%s", fname, lname);
    return 0;
}
// Concatenation string, Add string
Output: Hi, Golam Rabbani
#strchr(): Searches the occurrence of a specified
character in the given string.
e.g. a=”Md - Golamrabbani”;
b=”-”;
Result - Golamrabbani
Program:
#include<stdio.h>
#include<string.h>
int main(){
    char fname[55] = "Golam - Rabbani";
    char lname = '-';
    char *n;
    n=strchr(fname, lname);
    printf("%s", n);
    return 0;
}
// String Character
Output: - Rabbani
<<<<<<<<Thank you>>>>>>>
String | C Programming Strings (With Examples) String | C Programming Strings (With Examples) Reviewed by Golam Rabbani on November 30, 2018 Rating: 5

No comments:

top navigation

Powered by Blogger.