#include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { string str; char a; int k = 0; cin >> str; for (int i = 0; i < str.length() - 1; i++) { a = str.length() - 1; // проблема вот тут if (str[i] == a) k++; } cout << k; system("PAUSE"); return EXIT_SUCCESS; } 

It is necessary to find how many times the last letter of a word occurs in the word itself. I want to mark the last letter here by “a”, but I don’t know how. I can not guess. Tell me?

  • @ navi1893, You are also interested in counting the number of lines in the file. See below for my answer on this. - avp

3 answers 3

 #include <cstdlib> #include <iostream> using namespace std; int main(int argc, char *argv[]) { string str; int k = 0; cin >> str; for(int i = 0; i < str.length() - 1; i++) { if (str[i] == str[str.length()-1]) k++; } cout << k; system("PAUSE"); return EXIT_SUCCESS; } 
  • This is the first time I see. Thank you very much! - navi1893
  • to str[str.length()-1] you refer to str.length() times, although it could be reduced to a unit - Specter
  • @Spectre I think that any adequate compiler will optimize the length() call and make it out of the loop. Although, of course, it would be better to do it yourself. - Costantino Rupert
  • and how to make it in the file he checked the number of lines? I just don’t know how to label the lines, that is, I want to write: if "string" == "\ n") k ++; Tell me? How to mark the line? - navi1893

As promised, after football.

At first I thought of making a small training sketch for @ navi1893 , and I began to simply write options for counting the number of lines, but I became interested in the time of their execution.

I can not share the results.

C ++ program. I did it in MinGW g ++ on Win 7, and then in Linux g ++ under VBox on the same machine (I5-2500 3.30 Ghz).

So, the program itself (lines.cpp file) In it, in the comments, information about the execution time in Windows.

 #include <iostream> #include <cstdio> #include <cstdlib> #include <unistd.h> using namespace std; extern "C" long long mtime(); int main (int ac, char *av[]) { long long start = mtime(); int k = 0, ci, co = 0, how = av[1]? atoi(av[1]): 0; char c; string s; /* 10000001 lines (88888904 bytes) Windows 7 I5-2500 3.30 GHz */ switch (how) { case 0: cout << "getchar() countlines\n"; /* 700 msec */ while ((ci = getchar()) != EOF) { co = ci; if (ci == '\n') k++; } if (co && co != '\n') k++; break; case 1: cout << "cin.get(char) countlines\n"; /* 10300 msec */ while (cin.get(c) && !cin.eof()) { co = c; if (c == '\n') k++; } if (co && co != '\n') k++; break; case 2: cout << "read(0,char *,4096) countlines\n"; /* 450 msec */ int len, llen; char buf[4096]; llen = 0; while (len = read(0,buf,sizeof(buf))) { int i; for (i = 0; i < len; i++) { if (buf[i] == '\n') k++; } llen = len; } if (llen && buf[llen-1] != '\n') k++; break; default: cout << "getline(cin,string) countlines\n"; /* 14150 msec */ while (!cin.eof()) { getline(cin,s); if (cin.good() || s.length()) k++; } } cout <<k<< " lines\n" << mtime()-start <<" msec\n"; exit(0); } 

As you can see, 4 ways (almost the same) of counting the number of rows are implemented.

The program for creating a test file (file mkli.c)

 #include <stdio.h> #include <stdlib.h> int main (int ac, char *av[]) { int n = av[1]? atoi(av[1]): 10000000; if (n <= 0) n = 1000; printf ("n = %d\n",n); int i; for (i = 0; i < n; i++) printf ("%d\n",i); exit (0); } 

Text function for measuring time in milliseconds (mtime.c file)

 /* время в миллисекундах */ #include <stdio.h> #include <sys/time.h> long long mtime() { struct timeval t; gettimeofday(&t, NULL); long long mt = (long long)t.tv_sec * 1000 + t.tv_usec / 1000; return mt; } 

But the protocol for broadcasting and running programs in Linux (and the execution times in it)

 avp@avp-ubu1:~/hashcode$ avp@avp-ubu1:~/hashcode$ gcc -c mtime.c avp@avp-ubu1:~/hashcode$ gcc mkli.c -o mkli avp@avp-ubu1:~/hashcode$ g++ lines.cpp mtime.o -o lines avp@avp-ubu1:~/hashcode$ ./mkli >li.tst avp@avp-ubu1:~/hashcode$ wc li.tst 10000001 10000003 78888903 li.tst avp@avp-ubu1:~/hashcode$ avp@avp-ubu1:~/hashcode$ ./lines 0 < li.tst getchar() countlines 10000001 lines 685 msec avp@avp-ubu1:~/hashcode$ ./lines 1 < li.tst cin.get(char) countlines 10000001 lines 3101 msec avp@avp-ubu1:~/hashcode$ ./lines 2 < li.tst read(0,char *,4096) countlines 10000001 lines 225 msec avp@avp-ubu1:~/hashcode$ ./lines 3 < li.tst getline(cin,string) countlines 10000001 lines 2732 msec avp@avp-ubu1:~/hashcode$ 

I hope it will be informative.

UPDATE 1

Especially for @ navi1893 .

File navinl.cpp

 $ g++ navinl.cpp -o navinl $ ./navinl.exe < navinl.cpp 21 lines $ #include <iostream> #include <cstdio> using namespace std; int main (int ac, char *av[]) { int ci, co = 0, k = 0; while ((ci = getchar()) != EOF) { co = ci; if (ci == '\n') k++; } if (co && co != '\n') k++; cout <<k<< " lines\n"; return 0; } 

As for the variables ci and co .

Lines in the file are separated by '\ n'. It would seem that to determine the number of lines, it is enough to count the number of NEWLINE characters in the file.

However, if the last portion of the data in the file does not end with '\ n', then we will not consider it as a string (with the "obvious" algorithm). Therefore, after finding the end of the file, it is necessary to check whether the last character in the file '\ n' or not. For this purpose, we use the variable co , in which we save the last character read.

Why the libc getchar () IMHO is used for reading the file (standard input) is obvious from the previous material.

  • But is it possible for my particular case that you write and in a more understandable form? And that is so incomprehensible to me, and even the long odd comes out - navi1893
  • one
    Some kind of too bloated code turned out to be ... too ... - AseN
  • @Asen, too bloated for 4 variants of counting the number of lines? Maybe. I am pleased to look at the more compact. - avp
  • @avp apparently the TC does not want to learn, but wants to do for it * facepalm * - rasmisha
  • and even long even goes @ navi1893, you do not need everything, only case 2 - it is the fastest - Specter

Why all of you and try to go in sishny ways? This is not always good, and sometimes fatally bad. The for loop, for example, in your code - completely kills any, even scanty, flexibility ... it's good that you use the String class, not pointers to strings ...

 #include <iostream> #include <string> using namespace std; int main() { string word = "fuck yeah this is a sentense"; // наше слово char letter = word[word.size()-1]; // последняя буква int count = 0; int pos = word.find(letter); while(pos!=string::npos) { count++; pos = word.find(letter,pos+1); } cout<<count<<endl; cout<<"Letter '"<<letter<<"' returns in the word "<<count<<" times."<<endl; system("Pause"); } 

UPD:

 ifstream f("file.txt"); string s = ""; int c = 0; while(!f.eof()) { f>>s; c++; } 
  • 2
    @Asen Here, you are moving in the right direction. Using std::string.find is appropriate here and looks good. - Costantino Rupert
  • 3
    @ navi1893! [] [1] [1]: i.imgur.com/dQRxu.jpg - Costantino Rupert
  • 2
    @navi1893, print out before your for value file.eof (), calm down and think for half an hour. Then it will work out. - avp
  • one
    @avp @asen help somebody, otherwise I just can't do it! ( - navi1893
  • 2
    @ navi1893, after football. - avp pm