Thread: Is there any possible way to convert this program from C++ to C?

  1. #1
    Registered User
    Join Date
    May 2022
    Posts
    22

    Is there any possible way to convert this program from C++ to C?

    insert
    Code:
    
    #include <bits/stdc++.h>
    using namespace std;
     
    // Function to string into grid form
    void gridStr(string str)
    {
        int l = str.length();
        int k = 0, row, column;
        row = floor(sqrt(l));
        column = ceil(sqrt(l));
     
        if (row * column < l)
            row = column;
     
        char s[row][column];
        // convert the string into grid
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                s[i][j] = str[k];
                k++;
            }
        }
     
        // Printing the grid
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if (s[i][j] == '\0')
                    break;
                cout << s[i][j];
            }
            cout << endl;
        }
    }
     
    // Driver code
    int main()
    {
        string str = "GEEKSFORGEEKS";
        gridStr(str);
         
        return 0;
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Code:
    void gridStr(const char *str)
    {
        int columns = ceil(sqrt(strlen(str)));
        int i = 0;
        while (str[i]) {
            putchar(str[i++]);
            if (i % columns == 0) putchar('\n');
        }
        if (i % columns != 0) putchar('\n');
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  4. #4
    Registered User
    Join Date
    Oct 2019
    Posts
    82
    The code is actually very C-like.

    You only need to replace the usage of the C++ standard library with corresponding C API, do the same for the header files and use 'char *' in place of 'string str'. Well, that looks like pretty much it.

  5. #5
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Quote Originally Posted by ghoul View Post
    The code is actually very C-like.
    It's so C-like that it's not even proper C++! Variable length arrays are non-standard in C++ (and kind of garbage in C).
    And the code is massively over-complicated for the job.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  6. #6
    Registered User
    Join Date
    May 2022
    Posts
    22

    So I want to actually make a columnar transposition algorithm to encrypt the messages

    So basically it asks for a key and string. The length of the key determines how many columns in the matrix there are. However I call the strlen function but it doesn't work. It keeps on giving me the length of the string as 6.

    insert
    Code:
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include <math.h>
    
    
    int main(){
    	char str[50];
    	char key[50];
    	
    	printf("Please enter string:");
    	gets(str);
    	
    	printf("Please enter key:");
    	gets(key);
    	
        gridStr(str);
        
        return 0;
    }
    void gridStr(char *str, char *key)
    {	
    
    
    	int columns = (strlen(key));
        int i = 0;
        while (str[i]) {
            putchar(str[i++]);
            if (i % columns == 0) putchar('\n');
        }
        if (i % columns != 0) putchar('\n');
    }

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I want to convert this C program to C#
    By artistunknown in forum C# Programming
    Replies: 22
    Last Post: 03-20-2014, 05:57 AM
  2. Replies: 1
    Last Post: 03-16-2010, 10:17 AM
  3. Convert c++ program to c
    By tnt666 in forum C Programming
    Replies: 7
    Last Post: 12-01-2008, 09:23 AM
  4. Convert a C++ program?
    By unregistered in forum C Programming
    Replies: 4
    Last Post: 05-22-2002, 02:44 AM
  5. Can anybody convert this program.
    By andy bee in forum C Programming
    Replies: 3
    Last Post: 08-28-2001, 05:07 PM

Tags for this Thread