Read & Write using string (Name and Father name)

 import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
String s1, s2;

Scanner scan = new Scanner(System.in);
System.out.print("Please enter your name: ");
s1 = scan.nextLine();

System.out.print("Please enter your father name: ");
s2 = scan.nextLine();

System.out.println(s1+" "+s2);
}
}
=================


R1:Array&String

Write a program to read a string through keyboard and sort it using bubble sort

Write a program to read a string through keyboard and sort it using bubble sort.
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. int main(){
  5.    int i, j, n;
  6.    char str[25][25], temp[25];
  7.    printf("Enter the number of string : ");
  8.    scanf("%d", &n);
  9.  
  10.    printf("\nEnter %d strings : \n", n);
  11.    for(i=0; i<=n; i++)
  12.       gets(str[i]);
  13.      
  14.    for(i=0; i<=n; i++)
  15.       for(j=i+1; j<=n; j++) {
  16.          if(strcmp(str[i], str[j])>0) {
  17.             strcpy(temp, str[i]);
  18.             strcpy(str[i], str[j]);
  19.             strcpy(str[j], temp);
  20.          }
  21.       }
  22.    printf("\n\nSorted Strings :");
  23.    for(i=0; i<=n; i++)
  24.       puts(str[i]);
  25.   
  26.    return 0;
  27. }

Write a program to search a character in a string using binary search

Write a program to search a character in a string using binary search.
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void main()
  5. {
  6.     int i, n, start, end, mid;
  7.     char str[100][100], key[40];
  8.    
  9.     printf("Please enter the number of string to be stored : ");
  10.     scanf("%d",&n);
  11.    
  12.     printf("Enter the string in ascending order\n");
  13.    
  14.     for(i=0; i<=n-1; i++) {
  15.         scanf("%s", &str[i]);
  16.     }
  17.    
  18.     printf("Enter the string to be searched : ");
  19.     scanf("%s", &key);
  20.    
  21.     start = 0;
  22.     end = n-1;
  23.     while(start <= end) {
  24.         mid = (start+end)/2;
  25.         if (strcmp(key, str[mid])==0)
  26.             {
  27.                 printf("key found at the position %d\n", mid+1);
  28.                 exit(0);
  29.             }
  30.         else if
  31.             (strcmp(key, str[mid])>0)
  32.             {
  33.                 end = end;
  34.                 start = mid+1;
  35.             }
  36.            
  37.         else {
  38.                 start=start;
  39.                 end = mid-1;
  40.             }
  41.     }
  42.     printf("Failed\n");
  43.  }

Write a program to count total number of alphabets, digits or special characters in a string using loop

Write a program to count total number of alphabets, digits or special characters in a string using loop
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void main()
  5. {
  6.     char str[100];
  7.     int alph, digit, splch;
  8.     int len;
  9.         alph=0;
  10.         digit=0;
  11.         splch=0;
  12.         len=0;
  13.    
  14.     printf("Count total number of alphabets, digits and special characters :\n");
  15.     printf("Input the string : ");
  16.     gets(str);
  17.     len=strlen(str);
  18.  
  19.     for(int i=0; i < len; i++) {
  20.         if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
  21.             alph++;
  22.         else if (str[i] >= '0' && str[i] <= '9')
  23.             digit++;
  24.         else
  25.             splch++;
  26.     }
  27.     //Check each character of string
  28.     printf("Number of Alphabetics in the string is : %d\n", alph);
  29.     printf("Number of Digits in the string is : %d\n", digit);
  30.     printf("Number of Special characters in the string is : %d\n\n", splch);
  31. }

Write a program to read a sentence and replace lowercase characters by uppercase and vice-versa


Write a program to read a sentence and replace lowercase characters by uppercase and vice-versa
  1. #include<stdio.h>
  2. #include<string.h>
  3. void main()
  4. {
  5.     char str[100];
  6.     char ch;
  7.  
  8.     int len;
  9.     printf("Input the string: ");
  10.     gets(str);
  11.     len=strlen(str);
  12.  
  13.     for(int i=0; i<len; i++) {
  14.         ch = (islower(str[i])?toupper(str[i]):tolower(str[i]));
  15.     printf("%c", ch);
  16.     }
  17. }

Write a program to remove all repeated characters from a given string


Write a program to remove all repeated characters from a given string.
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. void main()
  5. {
  6.             char str[100];
  7.             int i, j, k;
  8.  
  9.             printf("Please enter any string :  ");
  10.             gets(str);
  11.                        
  12.             for(i = 0; i < strlen(str); i++) {
  13.                         for(j = i + 1; str[j] != '\0'; j++) {
  14.                                     if(str[j] == str[i]) {
  15.                                                 for(k = j; str[k] != '\0'; k++) {
  16.                                                             str[k] = str[k + 1];
  17.                                                 }
  18.                                     }
  19.                         }
  20.             }
  21.             printf("After removing all repeated characters = %s ", str);
  22. }


Write a program to find reverse of string

Write a program to find reverse of string
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. void main()
  5. {
  6.     char str[100];
  7.     char rev[100];
  8.  
  9.     int i, j, k;
  10.  
  11.     printf("Please enter a string : ");
  12.     scanf("%s", str); //gets(str);
  13.  
  14.     for(i = 0; str[i] != '\0'; i++);
  15.     {
  16.         k = i-1;
  17.     }
  18.     for(j = 0; j <= i-1; j++)
  19.     {
  20.         rev[j] = str[k];
  21.         k--;
  22.     }
  23.     printf("Reverse of the string is : %s\n", rev);
  24. }

Write a program to concatenate two strings specific element in an array without using library functions

Write a program to concatenate two strings specific element in an array without using library functions.

Using for loop
  1. #include <stdio.h>
  2. int main()
  3. {
  4.    char str1[100];
  5.    char str2[100];
  6.    int i, j;
  7.   
  8.    printf("please enter first string : ");
  9.    scanf("%s", str1);
  10.   
  11.    printf("Please enter second string : ");
  12.    scanf("%s", str2);
  13.  
  14.    for(i=0; str1[i] != '\0'; i++);
  15.  
  16.    for(j=0; str2[j] != '\0'; j++, i++) {
  17.       str1[i] = str2[j];
  18.    }
  19.  
  20.    str1[i] = '\0';
  21.   
  22.    printf("Concatenate is : %s", str1);
  23.   
  24.    return 0;
  25. }

Using while loop
  1. #include <stdio.h>
  2. int main()
  3. {
  4.    char str1[100];
  5.    char str2[100];
  6.    int i=0, j=0;
  7.   
  8.    printf("please enter first string : ");
  9.    scanf("%s", str1);
  10.   
  11.    printf("Please enter second string : ");
  12.    scanf("%s", str2);
  13.  
  14.    while(str1[i] != '\0')
  15.         i++;
  16.  
  17.    while(str2[j] != '\0'){
  18.        str1[i] = str2[j];
  19.        j++;
  20.        i++;
  21.    }
  22.    str1[i] = '\0';
  23.   
  24.    printf("Concatenate is : %s", str1);
  25.    return 0;
  26. }

Write a program to find length of a string without using library functions


Write a program to find length of a string without using library functions.
  1. #include <stdio.h>
  2. void main()
  3. {
  4.     char str[100];
  5.     int i, len = 0;
  6.  
  7.     printf("Please enter a string : ");
  8.     scanf("%s", str);
  9.    
  10.     for (i = 0; str[i] != '\0'; i++) {
  11.         len++;
  12.     }
  13.     printf("The length of a string is : %s = %d\n", str, len);
  14. }


Reverse of string using function

Input : howAre
Output: erAwoh



#include <stdio.h>
#include <string.h>

void getInverse(char str[]);

void main()
{
    char str[100];

    printf("Please enter string ");
    scanf("%s", str);
    getInverse(str);
    printf("Inverse string %s\n", str);
}

    void getInverse(char str[]){
        int i, j;
        char t;
     
        for(i=0, j=strlen(str); i<(strlen(str)/2); i++, j--){
            t=str[i];
            str[i]=str[j-1];
            str[j-1]=t;
        }
    }

Enter two string in separate line and output will be in one line

Input : Please enter the first string
            Alamin
            Please enter the second string
            Chowdhury
Output : Alamin Chowdhury



Used While loop
  1. #include <stdio.h>

  2. void main()
  3. {
  4.     char str1[100];
  5.     char str2[100];
  6.     char str3[100];
  7.     
  8.     int i=0, j=0, k=0;
  9.     printf("Please enter the first string\n");
  10.     //gets(str1);
  11.     scanf("%s", str1);
  12.     printf("Please enter the second string\n");
  13.     //gets(str2);
  14.     scanf("%s", str2);
  15.     
  16.     while(str1[i] != '\0'){
  17.         str3[k]=str1[i];
  18.         i++;
  19.         k++;
  20.     }
  21.     str3[k]=' ';
  22.     k++;
  23.     while(str2[j] != '\0'){
  24.         str3[k]=str2[j];
  25.         j++;
  26.         k++;
  27.     }
  28.     str3[k]='\0';
  29.     printf("%s\n", str3);
  30. }

Used for loop

  1. #include <stdio.h>

  2. void main()
  3. {
  4.     char str1[100];
  5.     char str2[100];
  6.     char str3[100];
  7.     
  8.     int i=0, j=0, k=0;
  9.     printf("Please enter the first string\n");
  10.     //gets(str1);
  11.     scanf("%s", str1);
  12.     printf("Please enter the second string\n");
  13.     //gets(str2);
  14.     scanf("%s", str2);
  15.     
  16.     for(str1[i] != '\0'; str3[k]=str1[i]; i++){
  17.         //str3[k]=str1[i];
  18.         //i++;
  19.         k++;
  20.     }
  21.     str3[k]=' ';
  22.     k++;
  23.     for(str2[j] != '\0'; str3[k]=str2[j]; j++){
  24.         //str3[k]=str2[j];
  25.         //j++;
  26.         k++;
  27.     }
  28.     str3[k]='\0';
  29.     printf("%s\n", str3);
  30. }


Count total number of alphabets, digits and special characters







  1. #include <stdio.h>
  2. #include <string.h>

  3. void main()
  4. {
  5.     char str[100];
  6.     int alph, digit, splch;
  7.     int len;
  8.         alph=0;
  9.         digit=0;
  10.         splch=0;
  11.         len=0;
  12.     
  13.     printf("\n\nCount total number of alphabets, digits and special characters :\n");
  14.     printf("---------------------------------------\n");
  15.     printf("Input the string : ");
  16.     gets(str);
  17.     len=strlen(str);
  18.   
  19.     for(int i=0; i < len; i++) {
  20.         if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
  21.             alph++;
  22.         else if (str[i] >= '0' && str[i] <= '9')
  23.             digit++;
  24.         else
  25.             splch++;
  26.     }
  27.     //Check each character of string
  28.     printf("Number of Alphabetics in the string is : %d\n", alph);
  29.     printf("Number of Digits in the string is : %d\n", digit);
  30.     printf("Number of Special characters in the string is : %d\n\n", splch);
  31. }

Convert lowercase to uppercase or vice versa

Input: aLAMiN
Output: AlamIn


  1. #include<stdio.h>
  2. #include<string.h>
  3. void main()
  4. {
  5.     char str[100];
  6.     char ch;

  7.     int len;
  8.     printf("Input the string: ");
  9.     gets(str);
  10.     len=strlen(str);

  11.     for(int i=0; i<len; i++) {
  12.         ch = (islower(str[i])?toupper(str[i]):tolower(str[i]));
  13.     printf("%c", ch);
  14.     }
  15. }