❶ c語言題:將s串中所有字元前移一個位置,第一個字元移到最後。謝謝幫
將s串中所有字元前移一個位置,第一個字元移到最後:
intlen=strlen(s);
charc=s[len-1];
s[len-1]=s[0];
s[0]=c;
(1)c語言將前幾個字元平移到最後擴展閱讀
字元串中字母順序前移,其他字元順序後移。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char*fun(char*s)
{
inti,j,k,n;
char*p,*t;
n=strlen(s)+1;
t=(char*)malloc(n*sizeof(char));
p=(char*)malloc(n*sizeof(char));
j=0;
k=0;
for(i=0;i<n;i++)
{
if(((s[i]>='A')&&(s[i]<='Z'))||((s[i]>='a')&&(s[i]<='z')))
{
t[j]=s[i];
j++;
}
else
{
p[k]=s[i];
k++;
}
}
for(i=0;i<k;i++)
t[j+i]=p[i];
t[j+k]=0;
returnt;
}
intmain()
{
chars[80];
printf("Pleaseinput:");
gets(s);
printf(" Theresultis:%s ",s);
printf(" Theresultis:%s ",fun(s));
getchar();
getchar();
return0;
}
❷ c語言字元串把最前面3個數移到最後面比如ABCDEFG變成DEFGABC
#include <stdio.h>
int main()
{
char s[1000];
int i;
gets(s);
for(i=3;s[i]!='\0';i++)
putchar(s[i]);
for(i=0;i<3 && s[i]!='\0';i++)
putchar(s[i]);
return 0;
}
請採納。
❸ C語言請編寫函數fun,其功能是:移動字元串中的內容,移動的規則是把第1~m個字元,平移到字元串的
#include <stdio.h>
#include <string.h>
#define N 80
void fun(char *w, int m);
void main()
{
char a[N]= "ABCDEFGHIJK";
int m;
printf("The original string:\n");
puts(a);
printf("\n\nEnter m: ");
scanf_s("%d",&m);
fun(a,m);
printf("\nThe string after moving:\n");
puts(a);
printf("\n\n");
}
void fun(char *w,int m)
{
int i,j,k;
char b;
k=strlen(w);//讀出字元串的長度
for(i=0;i<m;i++)
{
b=w[0];//保存每次變換前的第一個字元
for(j=0;j<k-1;j++)//將數組進行左移位
{
w[j]=w[j+1];
}
w[k-1]=b;//將保存的那個字元還原到數組的最後的位置
}
}
❹ c語言 如何將一個字元串的前n個字母後移至尾,其他的按順序前移
#include <stdio.h>
#include <string.h>//為了使用memcpy、strlen等字元串函數
#define N 5
int main()
{
char a[] = "abcdefghijk";
int i, j, m;
char b[N+1] = {'\0'};//b將成為一個中間儲存數組
memcpy(b, a, N);//把a的前N個字元復制到b中,存起來,以供後面使用
m = strlen(a)+1;//得出a數組的長度
for(i=0,j=N; j<m; i++,j++)
{
a[i] = a[j];//前N個字元之後的字元「按順序前移」
}
strcat(a, b);//把b追加到a後面,達成「前n個字母後移至尾」
printf(a);
return 0;
}
❺ C語言函數題,編寫函數將輸入字元串的前三個字母移到最後
#include<stdio.h>
#include<string.h>
#define MAXS 10
void Shift(char s[]);
void GetString(char s[]);/*實現細節在此不表*/
int main()
{
char s[MAXS];
GetString(s);
Shift(s);
printf("%s ",s);
return 0;
}
/*你的代碼將被嵌在這里*/
(5)c語言將前幾個字元平移到最後擴展閱讀:
include用法:
#include命令預處理命令的一種,預處理命令可以將別的源代碼內容插入到所指定的位置;可以標識出只有在特定條件下才會被編譯的某一段程序代碼;可以定義類似標識符功能的宏,在編譯時,預處理器會用別的文本取代該宏。
插入頭文件的內容
#include命令告訴預處理器將指定頭文件的內容插入到預處理器命令的相應位置。有兩種方式可以指定插入頭文件:
1、#include<文件名>
2、#include"文件名"
如果需要包含標准庫頭文件或者實現版本所提供的頭文件,應該使用第一種格式。如下例所示:
#include<math.h>//一些數學函數的原型,以及相關的類型和宏
如果需要包含針對程序所開發的源文件,則應該使用第二種格式。
採用#include命令所插入的文件,通常文件擴展名是.h,文件包括函數原型、宏定義和類型定義。只要使用#include命令,這些定義就可被任何源文件使用。如下例所示:
#include"myproject.h"//用在當前項目中的函數原型、類型定義和宏
你可以在#include命令中使用宏。如果使用宏,該宏的取代結果必須確保生成正確的#include命令。例1展示了這樣的#include命令。
【例1】在#include命令中的宏
#ifdef _DEBUG_
#define MY_HEADER"myProject_dbg.h"
#else
#define MY_HEADER"myProject.h"
#endif
#include MY_HEADER
當上述程序代碼進入預處理時,如果_DEBUG_宏已被定義,那麼預處理器會插入myProject_dbg.h的內容;如果還沒定義,則插入myProject.h的內容。
❻ 編程題:鍵盤輸入一個字元串,然後鍵盤輸入整數n,編寫函數fun,將字元串中前n個元素平移到字元串的最後
方法一:
void fun ( char *pstr, int steps )
{
int n = strlen( pstr ) - steps;
char tmp[max_len];
strcpy ( tmp, pstr + n );
strcpy ( tmp + steps, pstr);
*( tmp + strlen ( pstr ) ) = '\0';
strcpy( pstr, tmp );
}
方法二:
void fun( char *pstr, int steps )
{
int n = strlen( pstr ) - steps;
char tmp[max_len];
memncpy( tmp, pstr + n, steps );
memncpy(pstr + steps, pstr, n );
memncpy(pstr, tmp, steps );
}
主函數 輸入 我相信樓主自己應該能解決,只是寫了方法
。求分!!!
❼ 輸入一個字元串,把該字元串的前3個字母移到最後,輸出變換後的字元串.比如輸入「abcdef」,輸出為"defabc"
#include<stdio.h>
main()
{
char str[50],temp;
int i,j;
gets(str); /*請輸入不超過49個字元的字元串*/
for(i=0;i<3;i++)
{
j=0;
temp=str[j];
while(str[j+1])
{
str[j]=str[j+1];
j++;
}
str[j]=temp;
}
puts(str);
}
❽ C語言函數題,編輯一段函數,將輸入的字元串的前三個字母移到後面!
//函數,輸入字元串,返回字元串前三字母。
public function getStoreName($str){
$one = mb_substr($str, 0, 1, 'utf-8');
$two = mb_substr($str, 1, 2, 'utf-8');
$three = mb_substr($str, 2, 3, 'utf-8');
if(preg_match('/^[x7f-xff]+$/', $one)){
$one = getFirstCharter($one);
}
if(preg_match('/^[x7f-xff]+$/', $two)){
$two = getFirstCharter($two);
}
if(preg_match('/^[x7f-xff]+$/', $three)){
$three = getFirstCharter($three);
}
return $one.$two.$three;
}
(8)c語言將前幾個字元平移到最後擴展閱讀:
寫一個函數,將一個字元串中的母音字母復制到另一字元串,然後輸出。
代碼塊:
方法1:
#include <stdio.h>
#include <string.h>
char y(char x[], char y[]); //定義復制函數。
main()
{
char a[20], b[20];
gets(a); //輸入字元串。
b[20]=y(a, b); //調用復制函數。
puts(b); //輸出復制後的字元串。
return 0;
}
//母音復制函數。
char y(char x[], char y[])
{
int m, i, j;
m=strlen(x);
for (i=0, j=0; i<m; i++){
if (x[i]=='a'||x[i]=='A')
y[j++]=x[i];
else if (x[i]=='e'||x[i]=='E')
y[j++]=x[i];
else if (x[i]=='i'||x[i]=='I')
y[j++]=x[i];
else if (x[i]=='o'||x[i]=='O')
y[j++]=x[i];
else if (x[i]=='u'||x[i]=='U')
y[j++]=x[i];
}
y[j] = '