當前位置:首頁 » 編程軟體 » 找單詞編程題

找單詞編程題

發布時間: 2022-07-04 01:54:49

1. c語言編程 指針之尋找最長單詞 I題

#include <stdio.h>
#include <ctype.h>
char *fun(char *p)
{
int i=0,maxlenwordlen=0,wordbegin=-1,maxlenwordbegin=-1;

if(p)
{
for(;*(p+i)!='\0';)
{
while(isspace(*(p+i)))
{
i++;
}
wordbegin=i;
while(*(p+i)!='\0' && !isspace(*(p+i)))
{
i++;
}
if(i-wordbegin>maxlenwordlen)
{
maxlenwordbegin=wordbegin;
maxlenwordlen=i-wordbegin;
}
}
}
if(maxlenwordlen)
{
*(p+maxlenwordbegin+maxlenwordlen)='\0';
printf("最長單詞是:");
return (p+maxlenwordbegin);
}
else
{
printf("你的輸入不含有任何單詞!\r\n");
return p;
}
}
int main()
{
char TBuf[1024],*pword;
gets(TBuf);
pword=fun(TBuf);
while(*pword!='\0'&& *pword!=' ')
putchar(*pword++);
return 0;
}

2. 猜單詞 程序設計題

額 別告訴我你是南郵的 我這有個以前做的的 只是「猜單詞」這一個模塊的程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define max 1024
#define min 50

void main()
{
FILE *fp;
char c;
int i=0;
int j=0;
int x=0;
char z[]={0};

char str[max][min]={0};
if((fp=fopen("單詞庫.txt","r"))==NULL)
{
printf("can not open file word.txt\n");
exit(0);
}
c=fgetc(fp);
while(c!=EOF)
{
if(c!=' ')
{
str[i][j]=c;
j++;
}
else
{
i++;
j=0;
}
c=fgetc(fp);
}
//產生隨機數,0-i
srand((unsigned int)time(NULL));
int n=rand()/20;
while((n<0)||(n>i))
{
n=rand()%9;
}

x=strlen(str[n]);
printf("#######guess begin######\n");
printf("\n");
for(int g=0;g<x;g++)
strcat(z,"_");
for(int u=0;u<x;u++)printf("_ ");

char gs;

while(strcmp(z,str[n])!=0)

{ printf("請輸入字母\n");
scanf("%c",&gs);
getchar();

int c=0;c++;
int flag=0;
if(c>=4*x)break;
for(int i=0;str[n][i]!='\0';i++)
{
if(str[n][i]==gs)
{ z[i]=str[n][i];
printf(z,"\n");

flag=1;
break;
}
}
if(!flag)
{
printf(z,"字母%不在單詞中\n",gs);
}

}

if(strcmp(z,str[n])==0)
printf("恭喜你猜對了%s",z);

fclose(fp);
}

3. C編程題:提取字元串中的單詞

csdn已為您找到關於java提取字元串中的單詞相關內容,包含java提取字元串中的單詞相關文檔代碼介紹、相關教程視頻課程

4. c語言編程題 折半法找單詞

查找基本演算法
折半演算法:
intSearch_Bin(intdata[],intkey,intlength)
{ // 在數組data中折半查找其值等於key的數據元素
int low,high,mid;
low=0; // 置區間初值
high=length-1;
while(low<=high){
mid=(low+high)/2;
if (key==data[mid]) // 找到待查元素
return mid;
else
if (key<data[mid])high=mid-1; // 繼續在前半區間進行查找
else
low=mid+1; // 繼續在後半區間進行查找
}
return -1;// 順序表中不存在待查元素
}

5. 編程題:給定字元串s,其內容為英語長句,其中包含英語單詞,標點符號,空格等內容,每個英語單詞使用標

兩個思路:

  1. 逐個查找標點或者空格(連續分隔符算一個),然後分類存儲

  2. 直接用string.h(C語言)中的strchr和substr(這個需要自定義)來劃分單詞。

祝你成功。

6. 《編程題》單詞接龍求教java高人解答

  1. 先將單詞復制一份。拿你的例子說,5個單詞,由於每個可以出現兩次,那麼最終就是10個單詞。

  2. 構造圖結構,你把每個單詞當成一個節點,遍歷所有節點,能收尾相連且不包含節點的就連一根線,做成一個有向圖。

  3. 遍歷整個圖,找到最長的

由於你最多隻有20個單詞,這種方法是絕對可行的,相當於把所有可能的龍找出來並挑選最長的。


算我無聊,寫了份代碼,你參考下吧,輸入輸出我沒怎麼管哈


import java.util.ArrayList;

import java.util.Collections;

import java.util.List;


public class FindLongestDragon

{

public static void main(String args[])

{

getLongestDragon('a', "at", "touch", "cheat", "choose", "tact");

}

public static void getLongestDragon(char startChar, String ... nodesStr)

{

List<Node> nodes = new ArrayList<Node>();

for (String nodeStr : nodesStr)

{

nodes.add(new Node(nodeStr));

nodes.add(new Node(nodeStr));

}

//遍歷所有節點關系,構成圖

for (Node node1 : nodes)

{

for (Node node2: nodes)

{

if (node1 != node2)

{

checkIsLink(node1, node2);

}

}

}

//遍歷

for (Node node : getStartWithCharNodes(nodes, startChar))

{

Dragon dragon = new Dragon();

dragon.append(node);

dragon.findNextNode();

}

//輸出所有的龍

Collections.sort(Dragon.dragons);

for (Dragon dragon : Dragon.dragons)

{

System.out.println(dragon);

}

}

//b是否能和a相連

public static void checkIsLink(Node a, Node b)

{

String nameA = a.getName();

String nameB = b.getName();

//存在包含關系

if (nameA.endsWith(nameB) || nameB.startsWith(nameA))

{

return;

}


if (getLinkStr(a, b) != null)

{

a.addChild(b);

}

}

public static String getLinkStr(Node a, Node b)

{

String nameA = a.getName();

String nameB = b.getName();

//從第二個字元開始檢查是否相連

for (int i = 1, length = nameA.length(); i < length; i++)

{

String linkStr = nameA.substring(i);

if (nameB.startsWith(linkStr))

{

return linkStr;

}

}

return null;

}

private static List<Node> getStartWithCharNodes(List<Node> list, char startChar)

{

List<Node> nodes = new ArrayList<Node>();

int size = list.size();

for (int i = 0; i < size; i+=2)

{

Node node = list.get(i);

if (node.getName().charAt(0) == startChar)

{

nodes.add(node);

}

}

return nodes;

}

}


class Dragon implements Comparable<Dragon>

{

private List<Node> nodes = new ArrayList<Node>();

public static List<Dragon> dragons = new ArrayList<Dragon>();

public void findNextNode()

{

Node lastNode = nodes.get(nodes.size() - 1);

boolean hasNextNode = false;

for (Node nextNode : lastNode.getChildren())

{

if (!nodes.contains(nextNode))

{

hasNextNode = true;

append(nextNode);

findNextNode();

removeTail();

}

}

//找到了盡頭

if (!hasNextNode)

{

Dragon = this.();

if (!dragons.contains())

{

dragons.add();

}

}

}

private Dragon ()

{

Dragon dragon = new Dragon();

for (Node node : this.nodes)

dragon.append(node);

return dragon;

}

public void append(Node node)

{

nodes.add(node);

}

public void removeTail()

{

nodes.remove(nodes.size() - 1);

}

public String toString()

{

StringBuilder sb = new StringBuilder();

//展示所有的節點

String allNodeLinkStr = getAllNodeLinkStr();

sb.append(allNodeLinkStr).append(" ");

sb.append(allNodeLinkStr.length()).append(" ");

for (Node node : nodes)

{

sb.append(node.getName()).append(" ");

}

return sb.toString();

}

@Override

public int compareTo(Dragon o)

{

return o.getLength() - this.getLength();

}


public int getLength()

{

return getAllNodeLinkStr().length();

}


public String getAllNodeLinkStr()

{

StringBuilder sb = new StringBuilder();

//展示所有的節點

sb.append(nodes.get(0));

for (int i = 1, length = nodes.size(); i < length; i++)

{

Node node = nodes.get(i);

sb.append(node.getName().substring(FindLongestDragon.getLinkStr(nodes.get(i - 1), node).length()));

}

return sb.toString();

}

public boolean equals(Object o)

{

if (o instanceof Dragon)

{

Dragon d = (Dragon)o;

if (d.nodes.size() == this.nodes.size())

{

int length = this.nodes.size();

for (int i = 0; i < length; i++)

{

if (!d.nodes.get(i).getName().equals(this.nodes.get(i).getName()))

{

return false;

}

}

return true;

}

}

return false;

}

}


class Node

{

private List<Node> children = new ArrayList<Node>();


private String name;

public Node(String name)

{

super();

this.name = name;

}


public List<Node> getChildren()

{

return children;

}


public String getName()

{

return name;

}


public void addChild(Node e)

{

this.children.add(e);

}

public String toString()

{

return this.getName();

}

}

7. 一道c語言編程題,尋找字元串最長的單詞並輸出

這一行寫錯了,p1前要加個*號:
printf("%c",p1++);
要改成
printf("%c", *p1++);

看別人寫的程序很難懂,不如自己重寫一下,已經測試通過:

#include <stdio.h>
#include <string.h>

int main()
{
char s[128];
char *p1, *p2;
int max=0, len=0;

printf("Input a string: ");
gets(s);

p1=s;
for (int i=0; i<strlen(s); i++)
{
if (s[i]==' ') // 如果當前字元為空格,則比較當前單詞長度是否大於最大值,再將長度復位。
{
if (len>max)
{
max=len;
p2=p1;
}
len=0;
} else // 如果當前字元非空,如果當前長度為0,則表示新單詞。
{
if (len==0)
p1=&s[i];
++len;
}
}
while (*p2 && *p2!=' ')
printf("%c", *p2++);
}

8. VC++找單詞問題

#include <iostream>
#include <string.h>
using namespace std;void findshort(char *f1, char *f2)
{
int length = strlen(f1);
int flag = 0;
int short_len = 100;
int len = 0;
char *p = f1;
char *short_add;
int i; cout<<f1<<endl;
for(i = 0; i <= length; i++)
{
if(flag == 0 && f1[i] != ' ')
{
flag = 1;
len++;
p = &f1[i];
}
else if(flag == 1 && f1[i] != ' ' && f1[i] != '\0')
{
len++;
}
else if(flag == 1 && f1[i] == ' ' || f1[i] == '\0')
{
if(len < short_len)
{
short_add = p;
short_len = len;
}
len = 0;
flag = 0;
}
}
strncpy(f2, short_add, short_len);
f2[short_len] = '\0';
}int main()
{
char f1[100];
char f2[100];
cout<<"輸入一行字元串:"<<endl;
gets(f1);
findshort(f1, f2);
cout<<"最短單詞:"<<f2<<endl; return 0;
}

9. Java 由用戶輸入一個單詞,編程在一個文本文件中查找這個單詞

importjava.io.BufferedReader;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importjava.io.InputStreamReader;
importjava.io.UnsupportedEncodingException;
importjava.util.Scanner;
importjava.util.regex.Matcher;
importjava.util.regex.Pattern;
publicclassDay11_A{
privatestaticScannersc=null;
publicstaticvoidmain(String[]args){
sc=newScanner(System.in);
Filefile=newFile("K:/Test/TestRead.txt");
Stringstr=null,src=null;
if(file.canExecute()){
str=readFile(file);
}else{
System.out.println("文件不存在!");
return;
}
while(true){
System.out.println("請輸入想查找的單詞:over為結束查找!");
src=sc.nextLine();
if(src.equalsIgnoreCase("over"))
break;
System.out.println("查找結果:"+look(src,str));
}

}
privatestaticStringreadFile(Filefile){
BufferedReaderbr=null;
StringBuilderstu=newStringBuilder();
try{
br=newBufferedReader(newInputStreamReader(newFileInputStream(file),"GBK"));
for(Stringstr=br.readLine();str!=null;str=br.readLine()){
stu.append(str);
stu.append(System.lineSeparator());
}
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
if(br!=null){
try{
br.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
returnstu.toString();
}
privatestaticintlook(Stringregex,Stringstr){
Matchermat=Pattern.compile(regex).matcher(str);
intcount=0;
while(mat.find()){
count++;
}
returncount;
}
}

熱點內容
騰訊雲伺服器購買網址 發布:2025-02-11 21:37:46 瀏覽:60
安卓電話視頻怎麼投電視上 發布:2025-02-11 21:32:27 瀏覽:18
易簽到源碼 發布:2025-02-11 21:31:03 瀏覽:498
編程班會 發布:2025-02-11 21:27:19 瀏覽:738
ubuntu編譯fortran 發布:2025-02-11 21:21:59 瀏覽:201
雲伺服器寬頻單位 發布:2025-02-11 20:48:11 瀏覽:538
安卓數據線公頭是哪個 發布:2025-02-11 20:45:42 瀏覽:812
網址原始密碼是什麼 發布:2025-02-11 20:33:52 瀏覽:72
怎麼創建伺服器我的世界網易 發布:2025-02-11 20:18:36 瀏覽:467
伺服器電腦與客戶端的連接 發布:2025-02-11 20:18:32 瀏覽:36