Way To Long Words Codeforces Solution in C/C++

admin
4 minute read
0

 

Way To Long Words  Codeforces Solution

In this article, we delve into the solution for the Codeforces problem "Way To Long Words" (Problem 71A). Codeforces is a popular platform for competitive programming, offering a variety of algorithmic challenges to hone one's coding skills. The Way To Long Words problem, a classic in the realm of programming puzzles, presents an interesting challenge that tests both logic and coding proficiency. In this discussion, we explore the intricacies of the problem statement and provide a detailed solution approach to tackle it effectively.

Click the link to see detailed description

https://codeforces.com/problemset/problem/71/A


Sequential Description

  1. Main Function:
    • Declare integer variables n and s, and a character array str to hold a string of up to 100 characters.
    • Read an integer value into n which represents the number of strings to process.
      2. Loop Through Strings:
      • Enter a while loop that iterates n times.
        • Read a string into str.
        • Calculate the length of the string and store it in s.
        • Check if the length s is greater than 10.
          • If true, output the first character of the string, followed by s - 2, followed by the last character of the string.
          • If false, output the entire string as is.
        • Output a newline after processing each string.

    CODE

    C++

    #include<bits/stdc++.h>
    using namespace std;
    int main()
    {
        int n;
        char str[100];
        int s;
        cin >> n;
        while (n--)
        {
            cin >> str;
            s = strlen(str);
            if (s > 10)
            {
                cout << str[0] << s - 2 << str[s - 1];
            }
            else
            {
                cout << str;
            }
            cout << endl;
        }
        return 0;
    }

    C

    #include <stdio.h>
    #include <string.h>
    
    int main() 
    {
        int n;
        char str[100];
        int s;
        scanf("%d", &n);
        
        while (n--) 
        {
            scanf("%s", str);
            
            s = strlen(str);
            
            if (s > 10) 
            {
                printf("%c%d%c\n", str[0], s - 2, str[s - 1]);
            } 
            else 
            {
                printf("%s\n", str);
            }
        }
        
        return 0;
    }
    
    In this C version, we've replaced cin and cout with scanf and printf respectively to read and write values. The %s format specifier is used to read and print strings. The strlen function is used from the string.h library to get the length of the string.

    Note

    We highly recommend you to try hard yourself at first & then if you fail, you can only check the logic from our code. Don't Copy-Paste our code exactly .If you do this, your logic will not be improved/developed. So, try to realize the logic following our description.

    Post a Comment

    0Comments

    Post a Comment (0)