私はリンクされたリストのトピックには新しく、ユーザーが入力するたびに 'u'を追加する方法を考えるのは難しいです。どんな助けや助けも大歓迎です。リンクリストの繰り返しと新しい値の追加
プログラム入力:
./queensenglish
入力文字列?:私は黄色を愛する
プログラムの予想される出力:
シャラタン!私は女王の英語を使用します。
私はあなたが間違っている
char
Sを比較するstrcmp
を使用しているyellouw
#include <stdio.h>
#include <cs50.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
char myChar;
struct node* next;
} node;
node* stringToList(char* inputString)
{
node* first = malloc(sizeof(node));
first->myChar = 'a';
first->next = NULL;
node* current = first;
char* s;
for(s = inputString; *s != '\0'; s++)
{
node* newNode = malloc(sizeof(node));
newNode->myChar = *s;
newNode->next = NULL;
current->next = newNode;
current = newNode;
}
return first->next;
}
char* listToString(node* firstChar)
{
// get the length of the string
int totalLen = 0;
node* current = firstChar;
while(current != NULL)
{
totalLen++;
current = current->next;
}
char *newString = malloc(sizeof(char)*(totalLen+1));
char *iter = newString;
current = firstChar;
for (int i = 0; i < totalLen; i++)
{
*iter = current->myChar;
current = current->next;
iter++;
}
*iter = '\0';
return newString;
}
int main(void)
{
// Read in an input string
printf("Input string?: \n");
char* myString = GetString();
// Convert that string into a linked list
node* firstChar = stringToList(myString);
node* current = firstChar;
while(current != NULL){
if(strcmp(¤t->myChar, "o") == 0 || (strcmp(¤t->myChar, "O") == 0)
{
//insert a 'u' after every 'o'
//printf("Char: %c\n", current->myChar);
}
current = current->next;
}
// convert the list back into a string
char* newString = listToString(firstChar);
// print out the "corrected" string
printf("Charlatan! I use the Queen's English:\n");
printf("%s\n", newString);
}
'のstrcmp(&の電流> myChar、 "O")== 0 'それはUBだ - >'の電流> myChar ==「O '' – BLUEPIXY
insertNode:{'u'、NULL}、nextNode:current-> next、insertNode-> next = nextNode; current-> next = insertNode; – BLUEPIXY
'node * first = malloc(sizeof(node)); \t first-> myChar = 'a'; first-> next = NULL; ... first - > next; 'memory leak - >'ノードを最初に返します。 \t node * current = &first; ... return first.next; ' – BLUEPIXY