Good afternoon)) For the second day I’m trying to write this program, but I don’t get anything (((Help please. There is a condition that finds the longest word in the sentence. Now you need to delete words that consist of Latin letters. (You cannot use string and so on)

int main(int argc, char** argv) { setlocale(LC_ALL, "Russian"); char str[80]; cout << "Enter string: "; cin.getline(str,80); int i; //Выводим само длинно слово int lenght = strlen(str); int maxLen = 0; int index = 0; int count = 0; for (i = 0; i < lenght; i++) { if (str[i] != ' '){ count += 1; } else { if (count > maxLen) { maxLen = count; index = i - count; } count = 0; } } if (count > maxLen) { maxLen = count; index = i - count; } maxLen += index; printf("\n"); for (i = index; i < maxLen; i++){ //putchar(str[i]); cout << str[i]; } printf("\n"); bracket(str); 

So I tried to write something (n-time), but as always it does not work)

 int temp =0; for(int i=0; i < lenght; i++){ if (((str[i]>='a')&&(str[i]<='z'))||((str[i]>='A')&&(str[i]<='Z'))){ temp++; } else str[i-temp] = str[i]; lenght-=temp; } cout << lenght; for(int i=0;i<lenght;i++) cout << str[i]; return 0; } 
  • and you can use regulars ??? - Alex.B
  • "you can not use the string and the like" - why like? More specifically, what exactly can not be used? - PinkTux
  • @ Alex.B yes you can - Dasha Novikova
  • @PinkTux Well, it is advisable to make it as simple as possible, without using ready-made functions - Dasha Novikova
  • I admit the phrase can not be used <something> without a justification of the reasons only in matters with a label competition . - αλεχολυτ

3 answers 3

I do not know what a "function string" is, so here are both parts of the job separately, with minimal use of library functions. The code is a bit redundant for clarity. When deleting words (second part), spaces and punctuation marks remain in their places. Handling numbers is not provided.

 #include <string.h> #include <stdlib.h> #include <stdio.h> #include <ctype.h> int main( void ) { char str[] = "someяяя string with longюю, longest, short and shortest words"; char *s = str; char *longest_ptr = s; size_t longest_len = 0; char *word_ptr; size_t word_len; while( *s ) { /* пропускаем пробелы и знаки препинания */ while( *s && ( isspace( *s ) || ispunct( *s ) ) ) { s++; } /* ищем самое длинное слово */ word_len = 0; word_ptr = s; while( *s && !isspace( *s ) && !ispunct( *s ) ) { s++; word_len++; } if( word_len > longest_len ) { longest_len = word_len; longest_ptr = word_ptr; } } /* выводим самое длинное слово */ printf( "longest word (%zu chars): ", longest_len ); while( longest_len-- ) { putchar( *longest_ptr++ ); } putchar( '\n' ); /* теперь ищем слова с латиницей */ s = str; while( *s ) { while( *s && ( isspace( *s ) || ispunct( *s ) ) ) { s++; } longest_len = 0; /* длина текущего слова */ word_len = 0; /* количество латинских букв в нём */ word_ptr = s; while( *s && !isspace( *s ) && !ispunct( *s ) ) { if( ( *s >= 'a' && *s <= 'z' ) || ( *s >= 'A' && *s <= 'Z' ) ) { word_len++; } longest_len++; s++; } /* слово только из латиницы, двигаем оставшуюся строку на его место */ if( word_len == longest_len ) { /* * было бы логичней: * memmove( word_ptr, s, strlen( s ) + 1 ); * но раз задание для мазохистов... */ word_len = 0; while( *s ) { word_ptr[word_len++] = *s++; } word_ptr[word_len] = 0; s = word_ptr; } } printf( "string without latin-only words: '%s'\n", str ); return 0; } 
  • #include <string.h> without this library) - Dasha Novikova
  • @DashaNovikova, to remove the dependence on string.h you need to rewrite one line: memmove( word_ptr, s, strlen( s ) + 1 ); . Dare :) - PinkTux

It can be quite clumsy to check by character codes and if you have met at least one, then delete the entire input line, perhaps this is not entirely correct.

 int check_str(char * str) { //Большие 65-90 строчные 97-122 в dec ASCII int check = 0; for(int i =0; i<sizeof(check);++i) { check= static_cast<int>(str[i]); if(check>=65 || check<=90 || check>=97 || check<=122) str=""; } 
  • one
    The code snippet you provided does not make sense. :) - Vlad from Moscow
  • yes yes there is a joint) - Alex.B.

My three penny. :)

I would like to note that it is absolutely not necessary that the letters of the Latin alphabet follow each other without omission of codes. Therefore, in the general case, one cannot simply check the range ['a', 'z'] or ['A', 'Z']

The program below deletes words that contain at least one Latin letter. Together with such words, all other characters are deleted, which follow the word to be deleted and are before the next word.

 #include <iostream> #include <cstring> #include <cctype> int main() { const char *latin = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; const size_t N = 100; char s[N]; std::cout << "Enter a sentence: "; if ( std::cin.getline( s, sizeof( s ) ) ) { size_t n = std::strlen( s ) + 1; for ( char *p = s; *p; ) { while ( std::ispunct( ( unsigned char )*p ) || std::isspace( ( unsigned char )*p ) ) ++p; char *first = p; bool has_latin = false; while ( *p && !std::ispunct( ( unsigned char )*p ) && !std::isspace( ( unsigned char )*p ) ) { if ( !has_latin ) { has_latin = std::strchr( latin, std::toupper( ( unsigned char )*p ) ) != nullptr; } ++p; } if ( has_latin ) { while ( std::ispunct( ( unsigned char )*p ) || std::isspace( ( unsigned char )*p ) ) ++p; std::memmove( first, p, n - ( p - s ) ); n -= p - first; p = first; } } std::cout << s << std::endl; } return 0; } 

The dialogue with the program may look as follows:

 Enter a sentence: один one два two три three четыре four пять five один два три четыре пять