Posts

Showing posts with the label String

In Python, Tokenizing and Comparing Strings

Image
Download

In Java, Find Unique Words With Tree Set

Image
Download

In C Program, Count Number Of Times Text Repeats In User Input

Image
Download

In Python, Scramble String And Check If Two Strings Are Anagram

Image
from random import shuffle def scramble_letters(txt): chars = list(txt) shuffle(chars) return ''.join(chars) def is_anagram(s1, s2): return ''.join(sorted(list(s1))) == ''.join(sorted(list(s2)))

In Java, Recursively Count Upper Case Characters

Image
import java.util.Scanner; public class Test2{ private static int upCount(String line, int pos){ char c = line.charAt(pos); int n = ('A' <= c && 'Z' >= c) ? 1 : 0; if(line.length()-1 > pos){ n += upCount(line, pos+1); } return n; } public static void main(String[]args){ Scanner in = new Scanner(System.in); System.out.print("Enter string: "); String line = in.nextLine(); System.out.printf("No# of upper case: %d\n", upCount(line, 0)); } } Download

In Java, Multiple String Operations With Single Scanner Object

Image
 Problem Statement: Write a method named site. This method accepts the Scanner object as its parameter. This method reads a commercial website URL that starts with www and ends with .edu or .com. This method retrieves the name of the site and outputs the site without the www prefix and the domain suffix. If the user enters www.yahoo.com then this method outputs yahoo. Write a method called decrypt that accepts a Scanner object as its parameter. this method reads an encrypted word and then decrypts the word. Here is the decryption algorithm: only the even numbered characters should be counted as the part of the word. Other characters should be discarded. For example if the user enters: hwealxlaod then the decrypted word is HELLO. Pay attention that the decrypted word is all in capital letters. Write a method called reverse that accepts the Scanner object as its parameter. This method asks the user for the entire name and prints the name in the reverse order. You must only use nextL...

In Java, Sorted ArrayList Of Names In Title Case

Image
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main{ static List<String> personNames = new ArrayList<String>(); public static void main(String args[]){ Scanner scanner = new Scanner(System.in); String inputName; while (true){ System.out.println("Enter the next name:"); inputName = scanner.nextLine().trim(); if (inputName.toLowerCase().equals("end")){ break; } else { insertionSort(titleCase(inputName)); } } System.out.println(personNames.toString()); } /** * * It converts the given sting in titleCase. * * @param name * * @return * */ public static String titleCase(String name){ final char[]all = name.toCharArray(); StringBuilder s1 = new StringBuilder(all.length); boolean caps = true; for(char c : all){ if(Character.isSpaceChar(c)){ caps = true; } else if(caps){ c = Character.toUpperCase(c); caps = false; ...

In C++/Cpp, Split Full Name Into First, Middle And Last Name.

Image
#include<iostream> #include<string> using namespace std; void lastFirstMiddle(string full, string &first, string &middle, string &last){ size_t pos = full.find(','); last = full.substr(0, pos); full = full.substr(pos+2); pos = full.find(' '); first = full.substr(0, pos); middle = full.substr(pos+1); } void firstMiddleLast(string full, string &first, string &middle, string &last){ size_t pos = full.find(' '); first = full.substr(0, pos); full = full.substr(pos+1); pos = full.find(' '); middle = full.substr(0, pos); last = full.substr(pos+1); } int main(){ string full, first, middle, last; while(1){ cout << "Enter a full name: "; if(!(getline(cin,full))){ break; } if(string::npos == full.find(',')){ firstMiddleLast(full, first, middle, last); } else { lastFirstMiddle(full, first, middle, last); } cout << " " << "First name: ...

In C++, Transform String Filled With Asterisks In Between

Image
#include<iostream> using namespace std; int main(){ const int MAX_INPUT = 20; const char EOC = '\0'; char name[2 * MAX_INPUT]; cout << "Enter name: "; cin.getline(name, MAX_INPUT); int i; for(i = 0; i < MAX_INPUT && EOC != name[i]; i++); int j = (i * 2)-1; i --; name[j--] = EOC; while(i){ name[j--] = name[i--]; name[j--] = '*'; } cout << name << endl; return 0; } Download

With C++, Lookup Contacts For Given Partial Name

Image
#include<iostream> #include<cstring> using namespace std; int main() { const int LENGTH = 30; char contacts [][LENGTH] = {"Renee Javens, 678-1223", "Joe Looney, 586-0097", "Geri Palmer, 223-8787","Lynn Presnell, 887-1212", "Bill Wolfe, 223-8878", "Same Wiggins, 486-0998", "Bob Kain, 586-8712", "Tim Haynes, 586-7676", "John Johnson, 2223-9037", "Jean James, 678-4939", "Ron Palmer, 486-2783"}; const int SIZE = sizeof(contacts)/sizeof(contacts[0]); char search[LENGTH] = {""}; char repeat = 'y', found; int i; do{ cout << endl << "Search contacts: "; cin >> search; i = SIZE; found = 0; while(0 <= i){ if(strstr(contacts[i], search)){ found = 1; cout << "Found contact: " << contacts[i] << endl; } i --; } if(!found){ cout << "No...

Counting vowels in a string with C Sharp

Image
using System; public class CountVowels{ public static void Main(string[]args){ Console.Write("Enter a phrase: "); string line = Console.ReadLine(); if(null == line){ Console.WriteLine("\nError: Missing input phrase!"); return; } int vowels = 0; foreach(char c in line){ switch(c){ case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': vowels ++; break; } } Console.WriteLine("{0} vowels found in phrase: \"{1}\"", vowels, line); } } Download

Palindrome example with C programming

Image
#include<stdio.h> char lower(char c){ if('A' <= c && 'Z' >= c) return (c + 32); return c; } int alpha(char c){ c = lower(c); return (('a' <= c && 'z' >= c)); } int palindrome(char *str){ char *head = str, *tail = str; while(*tail) tail ++; tail --; while(head <= tail){ if(!alpha(*head)){ head ++; continue; } if(!alpha(*tail)){ tail --; continue; } if(lower(*head) != lower(*tail)) break; head ++; tail --; } return (tail < head); } void main(){ char sentence[200]; printf("Enter a message: "); fflush(stdin); gets(sentence); if(palindrome(sentence)){ printf("Palindrome\n"); return; } printf("Not a palindrome\n"); } Download

How to print reverse string in Java recursively?

Image
public class Reverse{ public static void main(String[]args){ doubleReverse("Java"); } public static void doubleReverse(String s){ int l = 0; if(null == s || 0 == (l = s.length())) return; char c = s.charAt(--l); System.out.printf("%c%c", c, c); s = s.substring(0,l); doubleReverse(s); } } Download

How to find unique words using python programming?

Image
words = list() print('Enter words:') while True: word = input().strip().lower() if 0 == len(word): break if word not in words: words.append(word) if words: print('Unique words given:') for word in words: print(word) else: print('No words given') Download

✔ Learn C in 2 hours ⚡ C Programming for beginners 🔥 Tecq Mate Tutorials ✌

Image

Car for Hire | Java Swing Car Hire Desktop Application

Image
Download code here .

Cpp/C++ read grades from file and show average grades per student

Image
Download code here main.cpp #include<iostream> #include<fstream> #include<iomanip> using namespace std; int readFile(const string &fileName, int studentId[], double grades[], int maxGrades); double minimumGrade(const double grades[], int numberGrades); void displayForStudent(int id, const int studentId[], const double grades[], int numberGrades); void displayReverse(const int studentId[], const double grades[], int numberGrades); double maximumGrade(const double grades[], int numberGrades); double averageGrade(const double grades[], int numberGrades); int main(){ const int max = 20; int studentId[max]; double grades[max]; string fileName; int count; int i; do{ cout << "Enter the file name to read or hit enter to exit" << endl; if(!getline(cin,fileName) || 0 == fileName.size()){ break; } count = readFile(fileName, studentId, grades, max); cout << "The...

Flowgorithm | Flow chart call user defined function

Image
Download solution here

C plus plus read and write plain text into file

Image
Download code here .

Transform/Convert date string yyyy/mm/dd to dd-mm-yyyy | Java Date String

Image
Download code here .