c++ - first and last char in multiple strings -
please guys need help! need compare first , last char in multiple strings. have no clue how that. strings kind of generated randomly.
for example: user types differents strings , program needs compare first , last charachter of each string . example: 3 strings. "money" "dad" "generating".
... goal positive value on "dad" , "generating" cuz first char , last char of string "dad" , "generating" same. begginer. please help.
here code have stuck. sry bad english. :(
#include <stdio.h> #include <string.h> #include <conio.h> #include <stdlib.h> char funkcija() { file *c; c = fopen("datoteka.txt", "r"); if (c == null) { printf("error in opening!"); getch(); exit(1); } int = 0; char polje[100]; while (fscanf(c, "%s", polje) == 1) { int duljina = strlen(polje); if (polje[0] == polje[duljina - 1]) { i++; } } printf("rezultat je: %d", i); } int main() { funkcija(); getch(); return 0; } first of thank help. rly sorry if asked wrong question or if wasnt clear enough. try put part of code botthers me most. if(array[0]==array[length-1]){ i++; } here`once again rly sorry bad explanation of problem :/
1) use std::string
2) put logic in function
3) check empty strings
#include <iostream> using namespace std; bool hasfirstandlastcharequal(const std::string& s) { if(s.empty()) return false; return s.front() == s.back(); } int main() { cout << hasfirstandlastcharequal("money") << endl; cout << hasfirstandlastcharequal("dad") << endl; cout << hasfirstandlastcharequal("generating") << endl; }
Comments
Post a Comment