将int n转换为c中长n的字符串

发布于 2025-01-23 18:43:22 字数 413 浏览 2 评论 0原文

地狱,我是C的新手,想了解字符串和整数转换。

我正在尝试编写一个函数,该函数采用整数n,转换为由n长n组成的字符串。然后根据字母的n数字将其转换回一个字符串。

例如,如果我输入INT 3,它将返回由3个长度3的字符串,因此“ 333”。然后,我想将其转换为“ CCC”,因为它是字母的第三个字母。

另一个示例是函数在整数5中的功能,然后返回“ eeeee”。 第5个字母的5个字母

到目前为止,

int *num = 3;
char* buffer[sizeof(int) * 4 + 1];  //got this from another question
sprintf(buffer, "%d", key_num)  //turn int into char

这是我的代码:将不胜感激

Hell I am very new to C and wanted to learn about strings and integer conversion.

I am trying to write a function that takes an integer n, converts to a string consisting of n's of length n. And then convert that back to a string based on the nth number of the alphabet.

For example, if i enter in int 3, it will return a string of length 3, consisting of 3, so "333". And then I would like to convert this into "CCC" since it is the 3rd letter of the alphabet.

Another example would be the function takes in the integer 5, and returns "EEEEE". 5 letters of the 5th letter of the alphabet

So far this is my code:

int *num = 3;
char* buffer[sizeof(int) * 4 + 1];  //got this from another question
sprintf(buffer, "%d", key_num)  //turn int into char

Anyhelp would be appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请别遗忘我 2025-01-30 18:43:22
#include <stdio.h>
#include <stdlib.h>

void number_to_alphabet_string(int n){

 char buffer[n];
 for(int i=0;i<n;i++){
   buffer[i] = n + 64;
   //check ASCII table the difference is fixed to 64
   printf("%c",buffer[i]);
   }
 printf("\n");}

 int main(void){
     int C = 3;
     number_to_alphabet_string(C);

     int J = 10;
     number_to_alphabet_string(J);
     return 0;
   }

如果您不熟悉 c (例如 c )的新手。通常,对于不使用复杂的语法和数据结构的任务,请尝试保持简单,C可以使您对类型的低水平控制,如果您将来可能需要进一步使用它,则需要掌握它。

#include <stdio.h>
#include <stdlib.h>

void number_to_alphabet_string(int n){

 char buffer[n];
 for(int i=0;i<n;i++){
   buffer[i] = n + 64;
   //check ASCII table the difference is fixed to 64
   printf("%c",buffer[i]);
   }
 printf("\n");}

 int main(void){
     int C = 3;
     number_to_alphabet_string(C);

     int J = 10;
     number_to_alphabet_string(J);
     return 0;
   }

exit

If you are new to a low level programming language like C and you care about it then I strongly suggest you to go through a textbook and learn it from scratch rather than combining snipets of code. In general try to keep it simple and for tasks like that don't use complex syntax and data structures, c gives you low level control of types and that's something that you need to master if you perhaps need to use it further in the future.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文